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

Viewed 73

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.

3 Answers

You can try this expression:

r'(?<=\d\s).*?(?=\s[A-Z])'

It uses:

lookbehind: has to start with number and whitespace.

lookahead: has to end on an upper case character.

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.

You can use

\d*,?\d+\s*(.*?)\s*\b[A-Z]+\b

See the regex demo. Details:

  • \d* - zero or more digits
  • ,? - an optional ,
  • \d+ - one or more digits
  • \s* - zero or more whitespaces
  • (.*?) - Group 1: any zero or more chars other than line break chars, as few as possible
  • \s* - zero or more whitespaces
  • \b[A-Z]+\b - one or more uppercase ASCII letters as whole word (\b are word boundaries).

See the Python demo:

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