开发者

Boolean logic in a single regular expression - possible?

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


use the | (pipe) feature:

^a|^b

Or, in extended formatting:

^a   # starts with an A
|    # OR
^b   # starts with a B


How about this: ^[ab]


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜