Take the 2-minute tour ×

Can we put into a single regular expression , boolean logic : line starts with 'a' or 'b' . Question is triggered by using FileHelpers utility which does have a text box "Record Condition Selector" for "ExcludeIfMatchRegex" . Utility is written in C#. ^a - works , just don't how write down ^a OR ^b

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

use the | (pipe) feature:


Or, in extended formatting:

^a   # starts with an A
|    # OR
^b   # starts with a B
share|improve this answer
Downvoter: how is this wrong? –  Robert P Nov 12 '09 at 0:08
Didn't work for me in FileHelpers. [ConditionalRecord(RecordCondition.ExcludeIfMatchRegex, "^\" | ^t")] –  MicMit Nov 12 '09 at 0:29
Spaces are significant in regexen. the regex "^\" | ^t" means: "match the start of string, a quote, followed by a space, OR match a space, then the start of string, then a 't'." Matching a space and then the start of string will always fail - it's impossible! :) Remove the spaces and it should work fine. –  Robert P Nov 12 '09 at 0:39
I didn't intend but managed to put spaces. And I didn't vote answer down. Yes, all works for my needs. [ConditionalRecord(RecordCondition.ExcludeIfMatchRegex, "^\"|^\t|^3 T")] –  MicMit Nov 12 '09 at 0:58
+1, but note that this won't match the whole line - just the first character. –  TrueWill Nov 12 '09 at 1:47
show 1 more comment

How about this: ^[ab]

share|improve this answer
My understanding it should work for single characters at the start, and it does. I needed substrings also which can be done using other answer. –  MicMit Nov 12 '09 at 0:25
Doesn't this have to be negated like so [^ab] as ^ outside a character class stands for the start of the line. –  Bruno Oct 23 '13 at 11:29
add comment

Having a hard time understanding you, but...if you're looking for a match if the string starts with "a" or "b", and a fail otherwise, you could do this:


Then, when you get the match's groups, the first group will be either an "a" or "b" and the second group will be the rest of the string.

share|improve this answer
I am not interested in FileHelpers internals, but your variant worked for me. [ConditionalRecord(RecordCondition.ExcludeIfMatchRegex, "^(\"|\t|3 T)(.+)$")] –  MicMit Nov 12 '09 at 0:21
add comment

A special construct (?ifthen|else) allows you to create conditional regular expressions. If the if part evaluates to true, then the regex engine will attempt to match the then part. Otherwise, the else part is attempted instead. The syntax consists of a pair of round brackets. The opening bracket must be followed by a question mark, immediately followed by the if part, immediately followed by the then part. This part can be followed by a vertical bar and the else part. You may omit the else part, and the vertical bar with it.

Check out conditionals page on regex.info for more details.

share|improve this answer
add comment

Your Answer


