开发者

Regular expression any character with dynamic size

开发者 https://www.devze.com 2023-02-12 17:36 出处:网络
I want to use a regular 开发者_StackOverflowexpression that would do the following thing ( i extracted the part where i\'m in trouble in order to simplify ):

I want to use a regular 开发者_StackOverflowexpression that would do the following thing ( i extracted the part where i'm in trouble in order to simplify ): any character for 1 to 5 first characters, then an "underscore", then some digits, then an "underscore", then some digits or dot.

With a restriction on "underscore" it should give something like that:

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

But i want to allow the "_" in the 1-5 first characters in case it still match the end of the regular expression, for example if i had somethink like:

to_to_123_12.56

I think this is linked to an eager problem in the regex engine, nevertheless, i tried to do some lazy stuff like explained here but without sucess.

Any idea ?


I used the following regex and it appeared to work fine for your task. I've simply replaced your initial [^_] with ..

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

It's probably best to replace your final * with + too, unless you allow nothing after the final '_'. And note your final part allows multiple '.' (I don't know if that's what you want or not).

For the record, here's a quick Python script I used to verify the 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\.]+$"

for 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: ALL a_12_1
Yes: ALL abc_12_134
Yes: ALL abcd_134_1.
Yes: ALL abcde_12_1
Yes: ALL a_123_123.456.7890.
Yes: ALL a_12_1
Yes: ALL ab_de_12_1


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

works for your example. The result doesn't change whether you use a lazy quantifier or not.


While answering the comment ( writing the lazy expression ), i saw that i did a mistake... if i simply use the folowing classical regex, it works:

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

Thank you.

0

精彩评论

暂无评论...
验证码 换一张
取 消