regex - Regular expression any character with dynamic size -


i want use regular expression following thing ( extracted part i'm in trouble in order simplify ): character 1 5 first characters, "underscore", digits, "underscore", digits or dot.

with restriction on "underscore" should give that:

^([^_]{1,5})_([\\d]{2,3})_([\\d\\.]*)$ 

but want allow "_" in 1-5 first characters in case still match end of regular expression, example if had somethink like:

to_to_123_12.56 

i think linked eager problem in regex engine, nevertheless, tried lazy stuff explained here without sucess.

any idea ?

i used following regex , appeared work fine task. i've replaced initial [^_] ..

^.{1,5}_\d{2,3}_[\d\.]*$ 

it's best replace final * + too, unless allow nothing after final '_'. , note final part allows multiple '.' (i don't know if that's want or not).

for record, here's quick python script used verify regex:

import re strs = [ "a_12_1",          "abc_12_134",          "abcd_123_1.",          "abcde_12_1",          "a_123_123.456.7890.",          "a_12_1",          "ab_de_12_1",        ] myre = r"^.{1,5}_\d{2,3}_[\d\.]+$"  str in strs:     m = re.match(myre, str)     if m:         print "yes:",         if m.group(0) == str:             print "all",     else:         print "no:",     print str 

output is:

yes: a_12_1 yes: abc_12_134 yes: abcd_134_1. yes: abcde_12_1 yes: a_123_123.456.7890. yes: a_12_1 yes: ab_de_12_1 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -