tags:

views:

125

answers:

4

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

+4  A: 

How about this: ^[ab]

Gyuri
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
+3  A: 

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:

^(a|b)(.+)$

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.

Jed Smith
I am not interested in FileHelpers internals, but your variant worked for me. [ConditionalRecord(RecordCondition.ExcludeIfMatchRegex, "^(\"|\t|3 T)(.+)$")]
MicMit
+2  A: 

use the | (pipe) feature:

^a|^b

Or, in extended formatting:

^a   # starts with an A
|    # OR
^b   # starts with a B
Robert P
Downvoter: how is this wrong?
Robert P
Didn't work for me in FileHelpers. [ConditionalRecord(RecordCondition.ExcludeIfMatchRegex, "^\" | ^t")]
MicMit
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
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
+1, but note that this won't match the whole line - just the first character.
TrueWill
Which is fine: he's not trying to match the whole line. If you take a look at his example, it's a C# attribute that "ExcludesIfMatchRegex". So if it starts with what he wants, he doesn't care about the rest.
Robert P
A: 

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.

Amarghosh