Search code examples
regexmatchingtex

match any number of consecutive words following a backslash


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.


Solution

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

    \\(test|other|list)\b