Home > Software engineering >  match any number of consecutive words following a backslash
match any number of consecutive words following a backslash

Time:07-28

I'm trying to match a TeX command, i.e. a backslash followed by a word (in a desired list) using regex, but with any number of them. For example, if the list I want is test, other, list, then the sequences \test, \other, and \list should be matched, while \sdfsdf should not. I also would want \test\list and \test\other\list to be matched. However, I don't want to match things like \testagain (although \test again should be). I tried the following regex

(\\)(test|other|list)([^a-z])

to no avail, since it does not match \test\other. How would I do this? I am not very experienced with regex.

CodePudding user response:

Use \b to match the word boundary at the end of the word.

\\(test|other|list)\b
  • Related