OR operator in Regular Expression
-
I am working on a C# project that looks for words in a text file. The current design is that my RegEx will need to be called 3 times to search for 3 different patterns: \bWord1\b then \bWord2\b followed by \bWord3\b Is there any OR operator that will allow me to specify all 3 words in a single RegEx pattern?
-
I am working on a C# project that looks for words in a text file. The current design is that my RegEx will need to be called 3 times to search for 3 different patterns: \bWord1\b then \bWord2\b followed by \bWord3\b Is there any OR operator that will allow me to specify all 3 words in a single RegEx pattern?
OR wont do it, look at (group) captures and it will match all. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
-
I am working on a C# project that looks for words in a text file. The current design is that my RegEx will need to be called 3 times to search for 3 different patterns: \bWord1\b then \bWord2\b followed by \bWord3\b Is there any OR operator that will allow me to specify all 3 words in a single RegEx pattern?
I recommend that you read about Regular Expressions[^] in the .NET Framework SDK. You really don't need to search 3 times, if I understand your logic correctly. You just need to find Word1, Word2, and Word3 one after another:
Regex re = new Regex(@"\bWord1\b.*\bWord2\b.*\bWord3\b"); // notice the "@"
Match m = re.Match("This is a sentence with Word1, then Word2, and followed by Word3.");This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]