Search code examples
regexregex-group

Regex how can i get only exact part in a string


I should only catch numbers which are fit the rules. Rules:

  1. it should be 16 digit
  2. first 11 digit can be any number
  3. after 3 digit should have all zero
  4. last two digit can be any number.

I did this way;

([0-9]{11}[0]{3}[0-9]{2})

number example: 1234567890100012

now I want to get the number even it has got any letter beginning or ending of the string like " abc1234567890100012abc" my output should be just number like "1234567890100012" When I add [a-zA-Z]* it gives all string.

Also another point is if there is any number beginning or ending of the string like "999912345678901000129999". program shouldn't take this. I mean It should return none or nothing. How can I write this with regex.


Solution

  • You can use look around to exclude the cases where there are more digits before/after:

    (?<!\d)\d{11}000\d\d(?!\d)

    On regex101