Regex Conditional Statement with Lookbehind -


so trying have regular expression uses lookbehind find previous character , have conditions on current character. if previous character 1 current character can 5-9 or if previous character 9, current character can 0-4, otherwise can digit. here regex trying not working.

[1-9]((?(?<=1)[5-9]|[0-9])|(?(?<=9)[0-4]|[0-9]))$

this should work if going embed in larger regex:

(?:[02-8]|1(?![^5-9])|9(?![^0-4]))+ 

the idea check consume characters:

  • for 0 , 2-8, there no restriction, consume them [02-8]
  • for 1, don't want consume if character ahead (if any) not 5-9, , (?![^5-9]) that.
  • same 9, (?![^0-4]) disallows 9 consumed if followed character other 0-4.

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -