-1

regex newbie here.

Say that I have the following string:

818y1873+130I have some text here09 124i10029102lsdgn2in3>31<
mkm1ikm12klni12m4j12??=?!=I"=and then some text heremsiJR310>39<0MK)==!)="!

What I would like to retrieve is the 31.

So, putting my query in English terms: after the string I have some text here followed by any character, the first (double) digits you find that are/is encapsulated by ><, retrieve that group for me.

I've tried the following pattern: r'I have some text here[\s\S\w\W\d\D]+>(\d{1,2})<', but it returns 39–the last occurence–see: https://regex101.com/r/cevLwj/1

What am I missing here? Thanks in advance!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Short answer, you are missing that its currently greedy with the `+`. If you want the quick fix use `+?`. But maybe someone can come up with a better pattern. – JvdV Aug 09 '20 at 16:21
  • 1
    Why are you using `[\s\S\w\W\d\D]`? `[\s\S]` means the same. – Toto Aug 09 '20 at 18:52

3 Answers3

0

You could use >(\d{1,2})< prepended by word characters \w and whitspaces \s.

Result: [\w\s]+>(\d{1,2})<

See working example: https://regex101.com/r/RUK9te/1

Webber
  • 4,672
  • 4
  • 29
  • 38
0

Try

I have some text here[^>]+>(\d\{1,2\})<

(Very likely will need to be tweaked to suit your input, but based on the information you have provided, this should be good enough.)

Happy Green Kid Naps
  • 1,611
  • 11
  • 18
0

This does the job:

^.*?>(\d\d?)<

Demo & explanation

Toto
  • 89,455
  • 62
  • 89
  • 125
  • Hi Toto! Thanks for you reply. In this case, `I have some text here` is relevant to me. Is there a way to apply this/start searching **after** a specific substring? – Kevin Siemons Aug 09 '20 at 19:04
  • You reckon the below is usable? `(?<=I have some text here.*?>(\d\d?)<` It seems to give me the correct results, however generalisability is important to me. Cheers – Kevin Siemons Aug 09 '20 at 19:09
  • @KevinSiemons: Yes, of course, you can use `(?<=I have some text here.*?>)\d\d?(?=<)` but only if your regex flavor supports variable length lookbehind, if it doesn't, use: `I have some text here.*?>(\d\d?)<` and the digits you're searching is in group 1. – Toto Aug 10 '20 at 06:35
  • Read and understood. Much obliged! – Kevin Siemons Aug 11 '20 at 13:17