Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I would like to retrieve the words between an amount and the first word in uppercase with a regex

Tags:

regex

I'm currently working with regular expressions. I know how to retrieve the euro amount with a regex:

r'[+-]?(?:[0-9]*[,])?[0-9]+'

I also know how to retrieve the word with CAPITAL letters:

r'\b[A-Z]+(?:\s+[A-Z]+)*\b'

But I don't seem to get the following to work. Retrieve the words between the euro amount and the first WORD in Capital. For example, if I have the following sentence:

€10,95 Hello how are you doing? good or, bad? WORD 

I want to retrieve: "Hello how are you doing? good or, bad?"

Any help is appreciated.

like image 778
Gerard B. Avatar asked Dec 17 '22 11:12

Gerard B.


1 Answers

With your shown samples, please try following regex. Written and tested in Python3x.

import re
text = '€10,95 Hello how are you doing? good or, bad? WORD '
val=re.search(r'€\d+(?:,\d+)?\s*(.*?)(?=\s+[A-Z])', text)
print(val.group(1))
Hello how are you doing? good or, bad?

Online demo for above regex

Explanation: Adding detailed explanation for above regex.

€\d+          ##Matching € character following 1 or more digits here.
(?:,\d+)?\s*  ##In a non-capturing group matching comma followed by 1 or more digits keeping it optional followed by 0 or more spaces.
(.*?)         ##Creating 1st(and only) capturing group with lazy match here.
(?=\s+[A-Z])  ##Doing positive look ahead to make sure its followed by spaces then capital letters.
like image 130
RavinderSingh13 Avatar answered Jun 11 '23 04:06

RavinderSingh13