Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find if a string contains substring with a number, some distance from another substring

I have a string like

s = 'ahlkhsa-anchorawO&B6o77ooiynlhwlkfnbla,)9;}[test:""ajanchorkh,2Yio98vacpoi [p{7}]test:"2"*iuyanchorap07A(p)a test:""*^g8p9JiguanchorLI;ntest:"9.3"'

i’d like to see if there exists a substring test:"some_number_here" that’s at most 5 characters following anchor

So in this case there is, test:"9.3" is following substring anchor after 5 other character.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There is also test:"2" but it’s too far away from the anchor before it, so I don’t want to count it.

(Bonus points if i could pull out the number inside test:"")

>Solution :

You may try

anchor.{,5}test:"([\d.]+)"
anchor            // literally "anchor"
.{,5}             // any character, repeats up to 5 times
test:"([\d.]+)"   // test:"digits and dots", the digits and dots are captured in group 1

The number is captured in group 1, see the test cases

See more about regex quantifiers

  • Use it in Python:
import re

s = 'ahlkhsa-anchorawO&B6o77ooiynlhwlkfnbla,)9;}[test:""ajanchorkh,2Yio98vacpoi [p{7}]test:"2"*iuyanchorap07A(p)a test:""*^g8p9JiguanchorLI;ntest:"9.3"'

res = re.findall(r'anchor.{,5}test:"([\d.]+)"', s)

print(res)  # ['9.3']
  • Note

The number matching is loose, test:"." also counts. If you want a more restricted number validation, you could try anchor.{,5}test:"(?!\.?")(\d*(?:\.\d*)?)"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading