dcsimg
www.webdeveloper.com
Results 1 to 5 of 5

Thread: Conditional regex

  1. #1
    Join Date
    Aug 2016
    Posts
    9

    Conditional regex

    Having following regex:

    Code:
    (?(?<=\AA)a|e)/g
    and test string:

    Code:
    Aa
    Ae
    Be
    it matches `a`(line #1), `e`(line #2), `e`(line #3).
    I think it should match just `a`(line #1) because there are 3 lines but only ONE string - `\AA` matches `A` only once (on the first line of the string) and therefore applies `a` pattern which matches only `a` in the fist line.

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,543
    RegEx is not my strongest subject but if its only matching one line, try the multi-line mode tag.

    Also there are variabces between languages, so please state what language this is, JavaScript, PHP, ASP, Java, C, etc. and the post can be moved to an appropriate forum.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Aug 2016
    Posts
    9
    Quote Originally Posted by \\.\ View Post
    RegEx is not my strongest subject but if its only matching one line, try the multi-line mode tag.

    It's not matching one line - it's matching the whole string (with newlines as \A anchor works). My question is how could the conditional match
    then
    (a) and
    else
    (e) clause at the same time if there is only one string starting with Aa (first line) and ending with Be (last line)?

    Also there are variabces between languages, so please state what language this is, JavaScript, PHP, ASP, Java, C, etc. and the post can be moved to an appropriate forum.
    it's pcre PHP

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,543
    I have moved to PHP as you should have posted in there, if its language specific, post in that forum for that language, you will get an answer much quicker.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,236
    Seems like the simplest PCRE regex would be /\AA(a|e)/

    Code:
    php > $text = <<<EOD
    <<< > Aa
    <<< > Ae
    <<< > Be
    <<< > EOD;
    php > $regex = '/\AA(a|e)/';
    php > preg_match_all($regex, $text, $matches);
    php > print_r($matches[1]);
    Array
    (
        [0] => a
    )
    php > print_r($matches[0]);
    Array
    (
        [0] => Aa
    )
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center