tags:

views:

169

answers:

3

For a text file, I want to match to the string that starts with "BEAM" and "FILE PATH". I would have used

^BEAM.*$
^FILE PATH.*$

if I were to match them separately. But now I have to concatenate those two matching patterns into one pattern.

Any idea on how to do this?

+5  A: 

A pipe/bar character generally represents "or" with regexps. You could try:

^(BEAM|FILE PATH).*$
zombat
+1  A: 

If the above post doesn't work, try escaping the () and | in different ways until you find one that works. Some regex engines treat these characters differently (special vs. non-special characters), especially if you are running the match in a shell (shell will look for special characters too):

  • ^\(BEAM|FILE PATH\).*$
  • %\(BEAM\|FILE PATH\).*$
  • etc.
Andy White
+1  A: 

The accepted answer is right but you may have redundancy in your Regular Expression.

  • ^ means match the start of a line
  • (BEAM|FILE PATH) - means the string "BEAM" or the string "FILE PATH"
  • .* means anything at all
  • $ means match the end of the line

So in effect, all you are saying is match my strings at the beginning of the line since you don't care what's at the end. You could do this with:

^(BEAM|FILE PATH)

There are two cases where this reduction wouldn't be valid:

  1. If you doing some with the matched string, so you want to match the whole line to pass the data to something else.

  2. You're using a Regular Expression function that wants to match a whole string rather than part of it. You can sometimes solve this by picking the a different Regular Expression function or method. For example in Python use search instead of match.

Dave Webb