Home:ALL Converter>conditional logic in regular expression

conditional logic in regular expression

Ask Time:2020-12-11T20:12:08         Author:Karthik Chintala

Json Formatter

I'm trying to get the following conditional logic with regular expression.

if (text starts with +61) {
  if (text after +61 is 1800) {
    // then match 6 digits after 1800. So, the regex until here should be +611800 and then 6 digits
  } else {
    // match 9 digits after +61
  }
}

I read on how to write conditional regular expressions. But, I'm stuck here with the following regular expression

^\+61((?=1800)\d{6}|\d{9})$

The above regex fails for this, but it has to be successful

  • +611800123456 (This has to be success)

And the following text passes the regex test but it has to fail

  • +61180012345 (This has to be fail)

Here are my tests in regexr site.

https://regexr.com/5ia2e

Author:Karthik Chintala,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65251156/conditional-logic-in-regular-expression
yy