Home > Software engineering >  Find if a string contains substring with a number, some distance from another substring
Find if a string contains substring with a number, some distance from another substring

Time:12-23

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.

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:"")

CodePudding user response:

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*)?)"

  • Related