How to find from certain words to empty lines with Regex

Asked 1 years ago, Updated 1 years ago, 233 views

How do I find a paragraph that starts with 002 in the paragraph below?

001 Hide and Seek/Can you see us? Can you see me? Yes, I can see you. Can you see me? Yes, we can see you. Can you see me? Yes, we can see you. We can all see Dad.

002 Reds and Blues/Red Team and Blue Team We are all in red. We are all in blue. Come on the reds! Red team, fighting! Come on the blues! Blue team, fighting! Who is in red? Who is in blue? We are all mud.

Come and look at this. Come and look at this. Is it a big monster? Come and look at this. Is it a big dinosaur? Come and look at this. Is it a big giant? No, it is Dad.

regex

2023-01-31 07:26

1 Answers

import re

# # Define the regular expression pattern for entity-label pairs
str = "001 Hide and Seek/Can you see us? Can you see me? Yes, I can see you. Can you see me? Yes, we can see you. Can you see me? Yes, we can see you. We can all see Dad.

002 Reds and Blues/Red Team and Blue Team We are all in red. We are all in blue. Come on the reds! Red team, fighting! Come on the blues! Blue team, fighting! Who is in red? Who is in blue? We are all mud.

Come and look at this. Come and look at this. Is it a big monster? Come and look at this. Is it a big dinosaur? Come and look at this. Is it a big giant? No, it is Dad.
"""


match = re.search(r'^002.*\n',str,re.MULTILINE)
print(match.group(0))

You can try it by pressing

.


2023-01-31 10:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.