user10561801
user10561801

Reputation:

need regex expression to avoid " \n " character

I want to apply regex to the below string in python Where i only want to capture Model Number : 123. I tried the below regex but it didn't fetch me the result.

string = """Model Number : 123 
            Serial Number : 456"""
model_number = re.findall(r'(?s)Model Number:.*?\n',string)

Output is as follows Model Number : 123\n How can i avoid \n at the end of the output?

Upvotes: 1

Views: 542

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626835

Remove the DOTALL (?s) inline modifier to avoid matching a newline char with ., add \s* after Number and use .* instead of .*?\n:

r'Model Number\s*:.*'

See the regex demo

Here, Model Number will match a literal substring, \s* will match 0+ whitespaces, : will match a colon and .* will match 0 or more chars other than line break chars.

Python demo:

import re
s = """Model Number : 123
            Serial Number : 456"""
model_number = re.findall(r'Model Number\s*:.*',s)
print(model_number) # => ['Model Number : 123']

If you need to extract just the number use

r'Model Number\s*:\s*(\d+)'

See another regex demo and this Python demo.

Here, (\d+) will capture 1 or more digits and re.findall will only return these digits. Or, use it with re.search and once the match data object is obtained, grab it with match.group(1).

NOTE: If the string appears at the start of the string, use re.match. Or add ^ at the start of the pattern and use re.M flag (or add (?m) at the start of the pattern).

Upvotes: 1

Shireen
Shireen

Reputation: 177

you can use strip() function

model_number.strip()

this will remove all white spaces

Upvotes: 0

Related Questions