Home > other >  Match group followed by group with different ending
Match group followed by group with different ending

Time:11-09

For example, let's say I have a list of words:

words.txt

accountable
accountant
accountants
accounted

I want to match "accountant\naccountants"

I've tried /(\n\w ){2}s/, but \w seems to be perfectly matching different things.

My RegEx also matches the following undesirable texts:

action
actionables
actionable
actions

Am I reaching out too far in what regex can do?

CodePudding user response:

You could for example use a capture group, and match a newline followed by a backreference to the same captured text and an s char.

If the first word can also be at the start of the string, instead of being preceded by a newline, you can use an anchor ^ instead.

^(\w )\n\1s$
  • ^ Start of string
  • (\w ) Capture group 1, match 1 word chars
  • \n\1s Match a newline, backreference \1 to match the same text as group 1 and an s char
  • $ End of string

Regex demo

  • Related