in reply to Re: combining RegEx
in thread combining RegEx

Can I use a string in brackets like so to match either 'Rel' or 'Release'?
'\s+Rel[ease]?\.?\s*([^\s;,\n\r]+)',

Replies are listed 'Best First'.
Re: Re: Re: combining RegEx
by japhy (Canon) on Nov 04, 2003 at 17:20 UTC
    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:??;

Re: Re: Re: combining RegEx
by ysth (Canon) on Nov 04, 2003 at 18:33 UTC
    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.

      Very much...Thanks :)