Quick PHP regex for digit format -


i spent hours figuring out how write regular expression in php need allow following format of string pass:

(any digit)_(any digit) 

which like:

219211_2 

so far tried lot of combinations, think 1 closest solution:

/(\\d+)(_)(\\d+)/ 

also if there way limit range of last number (the 1 after underline) amount of digits (ex. maximal 12 digits), nice.

i still learning regular expressions, appreciated, thanks.

the following:

\d+_\d{1,12}(?!\d) 

will match "anywhere in string". if need have either "at start", "at end" or "this whole thing", want modify anchors

^\d+_\d{1,12}(?!d)      - must @ start \d+_\d{1,12}$           - must @ end ^\d+_\d{1,12}$          - must entire string 

demo: http://regex101.com/r/jg0ez7

explanation:

\d+      - @ least 1 digit _        - literal underscore \d{1,12} - between 1 , 12 digits (?!\d)   - followed "something not digit" (negative lookahead) 

the last thing important otherwise match first 12 , ignore 13th. if number happens @ end of string , used form had [^\d] fail match in specific case.

thanks @sln pointing out.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -