3

Can anyone provide guidance on why the following regex is not working?

project = 'AirPortFamily'
line = 'AirPortFamily-1425.9:'
if re.findall('%s-(\d+):'%project,line):
    print line

EXPECTED OUTPUT:-

AirPortFamily-1425.9
3
  • Are there only digits after - and before :? Sep 20, 2018 at 4:26
  • @SamiKuhmonen and @Loocid yes,there are only digits after - and before but the digits can be of the format1425,1425.9,1425.9.111,1425.1.1.1
    – Ritz
    Sep 20, 2018 at 4:27
  • I mean, a dot isn’t a digit. Digit is not the same as a number that a human can understand. It’s a digit. So you’ll have to also accept the dot in them Sep 20, 2018 at 4:29

5 Answers 5

1

You should match the optional groups of digits preceded by a dot:

if re.findall(r'(%s-\d+(?:\.\d+)*):'%project,line):
10
  • How do we not capture the : at the end? Sep 20, 2018 at 4:33
  • Indeed I did not notice the : that the OP included in this original regex and left it there. I've removed it now. Thanks.
    – blhsing
    Sep 20, 2018 at 4:36
  • Yep that was only part had me stumped so explain to me how the asterisk works if you don't mind Sep 20, 2018 at 4:37
  • I mean I could google it but :) I just threw you and alley oop <3 Sep 20, 2018 at 4:37
  • The asterisk means to match zero or more of the preceding character or group, so in this case there can be optionally a number of groups of a dot followed by a string of digits.
    – blhsing
    Sep 20, 2018 at 4:40
1

Answer by Brandon looks good.

But in case there is a condition like "For a tag to be valid, it must end with a colon(:)"
In order to cover that condition, modifying Brandon's answer a little

project = 'AirPortFamily'
line = 'AirPortFamily-1425.9:'
matches = re.findall('%s-\d+\.+\d+\.*\d+:$'%project,line)
if matches:
    for elem in matches:
        print elem.split(':')[0]

Here is its working

#Matching lines with colon(:) at the end
>>> import re
>>> project = 'AirPortFamily'
>>> line = 'AirPortFamily-1425.9:'
>>> matches = re.findall('%s-\d+\.+\d+\.*\d+:$'%project,line)
>>> if matches:
...     for elem in matches:
...             print elem.split(':')[0]
...
AirPortFamily-1425.9 #Look, the output is the way you want.

#Below snippet with same regex and different line content (without :) doesn't match it
>>> line = 'AirPortFamily-1425.9'
>>> matches = re.findall('%s-\d+\.+\d+\.*\d+:$'%project,line)
>>> if matches:
...     for elem in matches:
...             print elem.split(':')[0]
...
>>> #Here, no output means no match
4
  • but if the line contains line = 'AirPortFamily-1425.9 ,it should not match it...it should only match the lines that contain :
    – Ritz
    Sep 20, 2018 at 4:58
  • Exactly. The regex that I suggested matches only the tags with colon (:) in the end ($) Sep 20, 2018 at 5:23
  • Oh, I got your requirement now. You want to match tags that ends with colon and as output, you want it to be printed without colon. I have modified my answer Sep 20, 2018 at 6:27
  • Please accpet an answer if any of them worked for you Sep 22, 2018 at 13:54
0

Your regex is missing the . after the first set of numbers. Here's a working example:

project = 'AirPortFamily'
line = 'AirPortFamily-1425.9:'
matches = re.findall('%s-\d+\.\d+'%project,line)
if matches:
    print matches
1
  • but if the line contains line = 'AirPortFamily-1425.9 ,it should not match it...it should only match the lines that contain :
    – Ritz
    Sep 20, 2018 at 4:59
0

On the possibilities with 1.1.1.1.1 I'm getting the : so just stripping it from the results

if re.findall(r'%s-[\d+\.\d+]+:'%project,line):
    print(line.strip(':'))
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 formats.py 
AirPortFamily-1425.9.1.1.1
3
  • but if the line contains line = 'AirPortFamily-1425.9 ,it should not match it...it should only match the lines that contain :
    – Ritz
    Sep 20, 2018 at 4:58
  • fixed that just added the : to the the regex again though your output doesn't desire the : to be included so we can strip it after we find it Sep 20, 2018 at 5:03
  • but this isnt matching something like AirPortFamily-1425.16:
    – Ritz
    Sep 20, 2018 at 5:12
0

This is my regex,my test on regex101 edit again

import re
project = 'AirPortFamily'
line = 'AirPortFamily-1425.9:AirPortfamily-14.5.9AirPortFamily-14.2.5.9:'
result = re.findall('%s[0-9.-]+:'%project,line) #this dose not cantain ':' [s[:-1] for s in re.findall('%s[0-9.-]+:'%project,line)]
if result:
    for each in result:
        print (each)
2
  • but if the line contains line = 'AirPortFamily-1425.9 ,it should not match it...it should only match the lines that contain :
    – Ritz
    Sep 20, 2018 at 4:59
  • it means only match begain with AirPortFamily- before :,the result of AirPortFamily-1425.9:AirPortfamily-14.5.9AirPortFamily-14.2.5.9: is AirPortFamily-1425.9: and AirPortFamily-14.2.5.9:?
    – KC.
    Sep 20, 2018 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.