Re: combining RegEx
by Abigail-II (Bishop) on Nov 04, 2003 at 15:13 UTC
|
/SW(?:=?|:)\s*([^\s;,\n\r]+)/
And the following ought to work too:
/SW[=:]?\s*([^\s;,\n\r]+)/
Abigail | [reply] [d/l] [select] |
Re: combining RegEx
by broquaint (Abbot) on Nov 04, 2003 at 15:13 UTC
|
Just use grouping, an alternation and a negative lookahead
my $pat = qr/
SW
(?: # the grouping + alternation
[:]
|
=? (?!ITCH) # ignore SWITCH
)
\s*
([^\s;,\n\r]+)
/x;
my %tests = (
'SW: foo' => 1,
'SW= bar' => 1,
'SW baz' => 1,
'SWITCH' => 0,
);
while(my($test,$res) = each %tests) {
print "result: ", ($res ? $1 : 'PASS'), "\n"
if $res == $test =~ $pat;
}
__output__
result: PASS
result: baz
result: foo
result: bar
See. perlre for more info.
| [reply] [d/l] |
Re: combining RegEx
by TomDLux (Vicar) on Nov 04, 2003 at 15:14 UTC
|
You want to recognise both RE1 and RE2. The string begins with SW, followed by either an optional '=' or a mandatory ':', any nunmber of spaces, and then the bit you want to capture, any continuous sequence of letters, numbers or puunctuation other than ';' and ','. Assuming the two RE work separately, you can combine them most easily with:
/SW[=:]?\s*([^\s;,\n\r]+)/
which looks for either '=' or ':' or neither.
--
TTTATCGGTCGTTATATAGATGTTTGCA
| [reply] [d/l] |
Re: combining RegEx
by Anonymous Monk on Nov 04, 2003 at 15:25 UTC
|
| [reply] |
Re: combining RegEx
by duff (Parson) on Nov 04, 2003 at 15:28 UTC
|
What doesn't work about it? It works just fine for me given what little I know about your input. I.e., when I feed it lines like "SW: foo", "SW= foo" and "SW foo", they all match. Also, since by "combining" the REs you've made the mandatory colon optional, you could just do this: /SW[:=]?\s*([^\s;,\n\r]+)/ That is, use a character class.
What do you mean "ignore the word SWITCH"? One way of ignoring words is making sure they are not there. Try s/SWITCH// prior to the match.
Jonathan Scott Duff
duff@pobox.com
| [reply] [d/l] [select] |
Re: combining RegEx
by ysth (Canon) on Nov 04, 2003 at 15:32 UTC
|
For some reason, it's hard to visualize the precedence in a regex; maybe it's the lack of whitespace (unless you use //x). Anyway, your attempt has a precedence problem.
It's parsed as a match of either /SW=?/ or /:?\s*(..../.
Enclose the | and its choices in (?: ) like so: /SW(?:=?|:?)\s*.../ though you don't need ? on both alternatives. In fact, you could say (?:=|:|) (using an empty alternative) or
(?:=?|:) or (?:=|:)? or [=:]?.
By "ignore the word 'SWITCH'" do you mean allow SWITCH or SW interchangably? If so, replace SW with SW(?:ITCH)?.
| [reply] |
|
|
Can I use a string in brackets like so to match either 'Rel' or 'Release'?
'\s+Rel[ease]?\.?\s*([^\s;,\n\r]+)',
| [reply] [d/l] |
|
|
No, you can't. Character classes are for characters, not strings. Use (?:ease)?.
And that other character class you have is a bit redundant; \s contains \n and \r.
_____________________________________________________
Jeff[japhy]Pinyan:
Perl,
regex,
and perl
hacker, who'd like a job (NYC-area)
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
| [reply] [d/l] |
|
|
No, [ease] is the same as [aes] and means one character which may be one of 'a', 'e', or 's'. What you want is the string ease modified by the "match either one or zero times" qualifier '?'. Since '?' has high precedence, saying ease? means the string "eas" optionally followed by 'e'. To make the '?' apply to the whole string "ease", you enclose it in parentheses (ease)?.
Since parentheses have two functions (to affect precedence and to cause capture of a submatch (e.g. into $1)) and you often want to do the former but not the latter, there is a special form of parentheses that don't capture: (?: before and ) after. This odd way of extending regex syntax was chosen since (? regex was otherwise meaningless and illegal.
Does that help?
You can install modules YAPE::Regex and YAPE::Regex::Explain and say (for example)
print YAPE::Regex::Explain->new("(ease)?")->explain() to get a verbose blow-by-blow description of what a particular regex does.
| [reply] [d/l] [select] |
|
|