I want to find the string right after a set of words using regex.
For example, if the words are "these" and "the",
thesePeople -> People
theApple -> Apple
Is it possible?
The main issue here is "the" is a substring of "these". I have read this question and made my attempt:
@"(?<=the|these)\w "
But this regex gives "sePeople" to me for the first case, and I cannot go further. Can anyone help?
CodePudding user response:
You can use this regex to get every character after the or these:
the(se){0,1}(\w )
The is always matched. If 'se' is also present, it is matched by the first capture group. Everything afterwards is then matched by the second capture group.
With this one you can also capture words that are not preceeded by the or these:
(?>the(?>se){0,1}){0,1}(\w )
CodePudding user response:
If all your words are formatted as above this ([A-Z]\w
) simple regex would work:
string[] words = new string[]{"thesePeople", "theApple"};
foreach(string word in words)
Console.WriteLine(Regex.Match(word, @"[A-Z]\w ", RegexOptions.None).Groups[0].Value);
Outputs:
People
Apple