concatenate multiple regexes into one regex

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?

-------------Problems Reply------------

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

^(BEAM|FILE PATH).*$

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.

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.
Category:regex Views:0 Time:2009-03-25
Tags: regex

Related post

  • Parse multiple XML tags using regex 2011-11-13

    I need to parse a few tags (their value) from am XML. This must be done by regex (don't ask me why :) ) For example: <name>AAA</name> <id>1234</id> <gender>M</gender> I know the pattern needed for each tag using re

  • How do I get multiple groups for a regex? 2012-01-27

    This code: string1 = "I will drill for a well in walla walla washington." /(w.ll) /.match(string1) is returning only will. Shouldn't it be returning will and well also? Check: http://rubular.com/r/48K8o5mzUX How do I get multiple groups for a regex i

  • How do I check for multiple date formats in Regex? 2010-12-07

    I am looking in a string to pull 2 date values in the format mm/dd/yyyy, m/d/yyyy, m/dd/yyyy, or mm/d/yyyy. How do I add that condition to my Regex? private static readonly Regex dateRegex = new Regex(@"\d{2}/\d{2}/\d{4}"); --------------Solutions---

  • How to concatenate multiple strings in android XML? 2011-04-23

    Problem I would like to be able to do something like that in Android XML code: <string name="title">@string/app_name test</string> <string name="title2">@string/app_name @string/version_name</string> Regarding first line of co

  • Python Regex vs PHP Regex 2008-09-22

    No not a competition, it is instead me trying to find why a certain regex works in one but not the other. (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01

  • Can I concatenate multiple MySQL rows into one field? 2008-11-10

    Using MySQL, I can do something like: SELECT hobbies FROM peoples_hobbies WHERE person_id = 5; and get: shopping fishing coding but instead I just want 1 row, 1 col: shopping, fishing, coding The reason is that I'm selecting multiple values from mult

  • Regex to validate regex 2009-01-06

    Possible Duplicate: Regexp that matches valid regexps Duplicate of Regexp that matches valid regexps How do you produce a regex that matches only valid regex? For instance: "[hc]at" would be valid (matching "hat" and "cat"), but "[hcat" would be inva

  • Yahoo-Pipes, Best Practices: 'Loop with a String Regex' vs. 'Regex' 2009-07-21

    What is a useful heuristic to consider when deciding between a 'Loop with a String Regex' and a 'Regex' module? --------------Solutions------------- http://discuss.pipes.yahoo.com/Message_Boards_for_Pipes/threadview?m=tm&bn=pip-DeveloperHelp&

  • C# Regex. This regex doesn't quite do the job 2009-10-06

    I'm trying to split these into tokens and it's mostly there. I really want to split the primary tokens up like NIGHT, set, the xpath pattern, the value to assign to the xpath pattern, i.e. in the second one, 'high' and in the second one, 'taken-offli

  • Concatenate multiple CSS files into one 2010-02-03

    What is the best way to concatenate multiple CSS files into one CSS file? I want to reduce the following .. <link href="css/1.css" rel="stylesheet" type="text/css" media="all"> <link href="css/2.css" rel="stylesheet" type="text/css" media="a

  • What is this Regex doing: new Regex(@"(? 2010-06-23

    Regex rx = new Regex(@"(?<!\\\\),"); String test = "OU=James\\, Brown,OU=Test,DC=Internal,DC=Net"; This works perfectly, but I want to understand it. I've been gooling without success. Can somebody give me a word or phrase that I can use to look t

  • What's the difference between vim regex and normal regex? 2010-10-05

    I noticed that vim's substitute regex is a bit different from other regexp. What's the difference between them? --------------Solutions------------- If by "normal regex" you mean Perl-Compatible Regular Expressions (PCRE), then the Vim help provides

  • do we ever use regex to find regex expressions? 2010-11-10

    let's say i have a very long string. the string has regular expressions at random locations. can i use regex to find the regex's? --------------Solutions------------- (Assuming that you are looking for a JavaScript regexp literal, delimited by /.) It

  • C++ regex: convert C# regex to C++ Linux 2011-01-18

    I can I convert exactly this regex in c#: Regex (@"\s*nameserver\s+(?<address>.*)") Regex (@"\s*search\s+(?<domain>.*)") to std::tr1::regex or to regex.h in C++? (Linux) thanks! --------------Solutions------------- std::tr1::regex in GCC

  • .NET Regex - Help with regex 'alternatives' (Pipe symbol | ) 2011-03-21

    I have a regex expression that I'm trying to construct that processes a file path and tries to find a file path whose directory ends with "Processed" or "Failed". I have something like this... static string PROCESSED_DIRECTORY = "Processed"; static s

  • Ruby Regex vs Python Regex 2011-04-15

    Are there any real differences between Ruby regex and Python regex? I've been unable to find any differences in the two, but may have missed something. --------------Solutions------------- The last time I checked, they differed substantially in their

  • better way to concatenate multiple strings in c? 2011-05-04

    Is there a better way to concatenate multiple strings together in c other than having multiple calls to strcat() all in a row, like below? char prefix[100] = ""; strcat(prefix, argv[0]); strcat(prefix, ": "); strcat(prefix, cmd_argv[0]); strcat(prefi

  • linux command to concatenate multiple files with content separated by filenames? 2011-07-21

    I am looking for a command that will concatenate multiple files in a directory tree with sames having a pattern such that the resulting file has contents of all the files separated by the name(path) of each file. I tried using find -exec and sed but

  • How do I concatenate multiple char strings in C? 2011-08-30

    Possible Duplicate: C String Concatenation How do I concatenate multiple char strings in C ? Example: const char *bytes = "tablr=Hello%20World"; const char *bytes2 = "tablr=Hello%20World"; const char *bytes3 = "tablr=Hello%20World"; thanks ----------

  • boost::regex vs std::regex - can't find empty() method? 2011-10-17

    Replacing boost::regex with std::regex since we are using gcc 4.6 in the company I ran into an issue with empty () method of that class - it basically didn't make it from boost::regex into std::regex class. I am not sure whether this is a gcc's issue

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.075 (s). 11 q(s)