Home > Software design >  Regex: Identify second character in a string should be equal to number(9)
Regex: Identify second character in a string should be equal to number(9)

Time:10-21

Am looking to identify the second digit that should be matching to digit 9 in a length of 19 numbers string, how to do that using Regex, please let me know.

Ex: 8934567890098765438

dentify the starting 2nd character should match 9 digit only and length of the string should be greater than 18

I have tried (?!^.[9])[0-9]{18}, [^.[9][0-9]{18} different ways, but am getting the right one.

CodePudding user response:

You can use a capture group to capture the 9 digit, and match 17 or more chars after it to match at least 19 digits in total:

^\d(\9)\d{17,}$

Regex demo

Or using a positive lookbehind, matching only a 9:

(?<=^\d)9(?=\d{17,}$)

Regex demo

CodePudding user response:

Use

^\d9\d{17}$

See regex proof.

With lookahead:

^(?=\d9)\d{19}$

See another regex proof.

When matching a substring of a longer string:

\b\d9\d{17}\b

(\b is a word boundary).

CodePudding user response:

^(?=.9)\d{19,}$

^(?=.9) : Second character is 9.

\d{19,}$: 19 or more digits

  • Related