Daniel Braun Daniel Braun - 10 months ago 60
Java Question

Mass alternating in regex

Regular alternating in regex :


This Is fine for few expressions but what if I have a quite large "word bank" from which I would like to construct the regex,
How would you suggest implementing it in a more elegant way?

Answer Source

Here are 3 possible options

  1. Create and search a dictionary

Have a look here to see how to use dictionaries in this application Java: Check if a string is in a dictionary

  1. Create and search an array


See here How can I test if an array contains a certain value?

  1. If the list are all mutations of the same word a regex may work after all. For example if your actual terms are (expression1|expression2|expression3) you could search it with /^expression[1-3]$/

You can experiment with Regex here: https://regex101.com/