简体   繁体   中英

Capturing words and number in parenthesis after a specific word

I am using regex to find the value using the keyword ' interest at the rate ' from this interest at the rate of ten percent (10%)

I tried this

re.compile(r'interest at the rate\s+((?:\w+(?:\s+|$)){3})').findall(r.decode('utf-8'))

and obtained ['of ten percent '] .

Now, I tried

re.compile(r'interest at the rate of\s+((?:\w+(?:\s+|$)){3})').findall(r.decode('utf-8'))

However, all I get is an empty value, [] .

How to get the number 10 from the above line? I want to capture three to four words after the keyword and get the integer value.

How to get the number 10 from the above line? I want to capture three to four words after the keyword and get the integer value

So, I understand that you expect to get three to four words after the keyword (= of ten percent ) and the integer value (= 10 ). I assume the "keyword" is interest at the rate , just what you used in the pattern.

Then, you may use

import re
s = "interest at the rate of ten percent (10%)"
r = re.compile(r'interest at the rate (\w+(?:\s+\w+){2,3})\s*\((\d+)')
print(r.findall(s))
# => [('of ten percent', '10')]

See the Python demo .

Details

  • interest at the rate - the keyword
  • (\\w+(?:\\s+\\w+){2,3}) - Group 1: one or more word chars and then 2 or 3 sequences of 1+ whitespaces followed with 1+ word chars
  • \\s* - 0+ whitespaces
  • \\( - a (
  • (\\d+) - Group 2: one or more digits.

If the number of words can be more than 2 or 3 or can be 1 or 0, replace {2,3} with * .

If the number can be a float, too, replace \\d+ with \\d[\\d.]* .

Okay, if I understand the question you can use the following

import re

value = "interest at the rate of ten percent (10%)"
regexString = r"^interest at the rate of ten percent \(([0-9]{2})%\)$"

result = re.findall(regexString, value, 0) # Zero is the flag for match all, you can omit this. 

print(result)

This will return ['10'] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM