8 Replies - 187 Views - Last Post: 04 December 2017 - 10:30 AM Rate Topic: -----

#1 Mulligan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-October 17

Conditional regex

Posted 04 December 2017 - 03:33 AM

Having following regex:

(?(?<=\AA)a|e)/g


and test string:

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Conditional regex

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4139
  • View blog
  • Posts: 13,085
  • Joined: 08-June 10

Re: Conditional regex

Posted 04 December 2017 - 03:47 AM

Quote

I think it should match just `a`(line #1)

Nope. The RegExp reads as: match a if it's preceded by A, or e.

cf. operator precedence, e.g. https://msdn.microso...c(v=vs.90).aspx
Was This Post Helpful? 0
  • +
  • -

#3 Mulligan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-October 17

Re: Conditional regex

Posted 04 December 2017 - 04:48 AM

View PostDormilich, on 04 December 2017 - 03:47 AM, said:

Quote

I think it should match just `a`(line #1)

Nope. The RegExp reads as: match a if it's preceded by A, or e.
I don't think You're right. The RegExp reads: if the first char of the string is A then match a else match e(check https://www.regular-...onditional.html and https://regex101.com/). So it should match only a in the first line because the condition is met (we have one string starting with A).
cf. operator precedence, e.g. https://msdn.microso...c(v=vs.90).aspx


View PostMulligan, on 04 December 2017 - 04:47 AM, said:

View PostDormilich, on 04 December 2017 - 03:47 AM, said:

Quote

I think it should match just `a`(line #1)

Quote

Nope. The RegExp reads as: match a if it's preceded by A, or e.

I don't think You're right. The RegExp reads: if the first char of the string is A then match a else match e(check https://www.regular-...onditional.html and https://regex101.com/). So it should match only a in the first line because the condition is met (we have one string starting with A).

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6440
  • View blog
  • Posts: 26,050
  • Joined: 12-December 12

Re: Conditional regex

Posted 04 December 2017 - 04:50 AM

Rather than struggling with the Quote options, add new information by scrolling further down the page to find the REPLY button.
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1863
  • View blog
  • Posts: 5,821
  • Joined: 15-January 14

Re: Conditional regex

Posted 04 December 2017 - 09:48 AM

First, how are you executing this? You've posted this in the PHP section, but your regular expression uses a g modifier and I don't see that as one of the options for PREG. So, how are you running that?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4139
  • View blog
  • Posts: 13,085
  • Joined: 08-June 10

Re: Conditional regex

Posted 04 December 2017 - 09:49 AM

The g modifier is often used in Java­Script.
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1863
  • View blog
  • Posts: 5,821
  • Joined: 15-January 14

Re: Conditional regex

Posted 04 December 2017 - 09:54 AM

That's what I figured, that's why I was wondering why it's in the PHP section.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4139
  • View blog
  • Posts: 13,085
  • Joined: 08-June 10

Re: Conditional regex

Posted 04 December 2017 - 09:56 AM

Who knows ... but that doesn't change the operator precedence in any way.
Was This Post Helpful? 0
  • +
  • -

#9 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1863
  • View blog
  • Posts: 5,821
  • Joined: 15-January 14

Re: Conditional regex

Posted 04 December 2017 - 10:30 AM

I understand that, the e is matching because that's what it's telling it to match, I just wanted to get clarification, for OP also.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1