I want to parse the line number one in the following paragraph using regex.
text=" My name is Raj
and I am an engineer."
Can someone help me with the regex to fetch the statement("My name is Raj") from the paragraph
CodePudding user response:
The general pattern for parsing a single line in regex is to match a specific pattern of characters that define the line, followed by a capturing group that captures the contents of the line. For example, if you wanted to parse a line that starts with "Hello," followed by a string of characters, you could create a regex pattern like so:
/^Hello,(.*)$/
The ^
character indicates the start of the line and the $
character indicates the end of the line. The .*
pattern will match any characters that come after the Hello,
. Finally, the parentheses around the .*
create a capturing group which will capture the contents of the line.
CodePudding user response:
To get the first line:
text=''' My name is Raj
and I am an engineer.'''
s=text.split('\n')[0]
print(s)
#Output:
#My name is Raj