javascript - Number grouping regex -


i have following regex:

\b((?:12345)(\d{1,4})1)\b 

what i'm trying make end of number must 1 regardless of range or choice of numbers in between. i'm having difficult time figuring out how around example 12345 111 1,(note: i've separated grouped numbers avoid confusion). how can make regex can tell numbers in range group apart '1' follows group?? thank help.

update:

no specific programming language, regex being stored in database , being pulled , used reference check javascript.

update:

some examples clarify:

-a user enters 1234511, how regex engine know if that's valid or not?? i.e. how engine know if it's valid 12345 1 1(a single '1' required ending '1') or invalid 12345 11 (the group of 1's part of (\d{1,4}) portion of regex, string doesn't include '1' @ end)

-a user enters 1234510111, valid. 12345 1011 1(the '1011' part of group (\d{1,4})) , includes 1 @ end.

summary:

the regex must recognize group of numbers ranging 1-4, string must end 1

the regex captures numbers capturing group:

var str = "123459991"; var match = str.match(/\b((?:12345)(\d{1,4})1)\b/);  alert(match[2]); // 999 

capturing groups parentheses have on regex.

  • group 0 has whole match by default (always matches[0], every regular expression)
  • group 1 ((?:12345)(\d{1,4})1), whole match again (not useful).
  • group 2 (\d{1,4}), number you're looking for.
  • 12345 not captured group - (?...) non-capturing group, , isn't included in token in results. again, doesn't add much, simple write 12345.

you can simplify regex /\b12345(\d{1,4})1\b/ , not lose information.

how matched:
generally, it's important remember regex engine tries hard match input. specifically, tries combinations (and possible starting positions) until can match pattern text.
example, if text 1234599991, matching easy:

  • match 12345 (character character, of course)
  • match \d{4} against 9999
  • match 1

next example: 123459991

  • match 12345
  • match \d against 9
  • match \d against 9
  • match \d against 9
  • match \d against 1
  • try match 1, fail (it consumed \d{4})
  • backtrack last matched character:
    • no \d{1,4} matched 999
    • 1 can matched.
  • done.

see also: backtracking, greediness , laziness


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -