Regex how can i get only exact part in a string

Viewed 34

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.

2 Answers

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

You can use a capture group, and match optional chars a-zA-Z before and after the group.

To prevent a partial match, you can use word boundaries \b or if the string should match from the start and end of the line you can use anchors ^ and $

\b[a-zA-Z]*([0-9]{11}000[0-9]{2})[a-zA-Z]*\b

Regex demo

Related