Regular expression between varying number of characters

The following regex matches "EXAMPLETEXT" in my example below, but I would like to use \d*

one or more digits to match instead \d\d

. Is this possible or is there a better method?

Line:

09.04.EXAMPLETEXT.14

      

Regex:

(?<=\.\d\d\.)(.*)(?=\.)

      

+3


source to share


2 answers


You don't need to use lookbehind at all for this, regex like this works:

(?:\d+\.)+(.*)(?:\.\d+)+

      

https://regex101.com/r/pP6lX7/1



Or if you want to match a line inside some other text

(?:\d+\.)+([^\s]*)(?:\.\d+)+

      

https://regex101.com/r/sB5mB2/1

+2


source


You cannot use quantifiers *

or +

or ?

inside lookbehind assertions in perl, java, python regexes (they will not support variable lookbehind lengths). But you can use these characters inside lookbehind in C # family.

If you are using php or perl you can use \K

\.\d*\.\K(.*)(?=\.)

      

Another hack in all languages ​​- just print all captured characters.

\.\d*\.(.*)\.

      

An example for the greedy one:



>>> s = "09.043443.EXAMPLETEXT.14"
>>> re.search(r'\.\d*\.(.*)\.', s).group(1)
'EXAMPLETEXT'

      

An example for non-greedy matching:

>>> re.search(r'\.\d*\.(.*?)\.', s).group(1)
'EXAMPLETEXT'

      

Use negative char class.

>>> re.search(r'\.\d*\.([^.]*)\.', s).group(1)
'EXAMPLETEXT'

      

+4


source







All Articles