Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. OR operator in Regular Expression

OR operator in Regular Expression

Scheduled Pinned Locked Moved C#
regexcsharpdesignquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Azel Low
    wrote on last edited by
    #1

    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?

    L H 2 Replies Last reply
    0
    • A Azel Low

      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?

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • A Azel Low

        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?

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        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]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups