How do I make part of a regular expression optional in Ruby?

后端 未结 4 937
无人及你
无人及你 2020-12-06 01:32

To match the following:

On Mar 3, 2011 11:05 AM, \"mr person\" 
wrote: 

I have the following regular expression:

/(On.* (?         


        
4条回答
  •  失恋的感觉
    2020-12-06 01:37

    Others got your answer. This is just an aside re: Regular Expressions.

    When you say "conditions" in regular expressions, it refers to the regex language. Like any language, its a branch in code execution, but the code is a different regular expression path, the "code" of regular expressions.

    So in psudo code: if (evaluation is true) do this regular sub-expression, else do this other sub-expression.

    This conditional exists in advanced regular expression engines ... Perl.
    Perl uses the most advanced regular expression engine that exists. In version 6 and beyond it will be an integral part of the language, where code and expression intermingle seamlessly.

    Perl 5.10 has this construct:
    (?(condition)yes-pattern|no-pattern).

    Edit Just a warning that where Perl goes, every other language follows as far as regular expression.

提交回复
热议问题