简体   繁体   中英

python search word after match dash

ihi, I want to search for a following word after a match, so when if i search in a string "i use a table blue" with (\\w+) with that regex i solver the problem, but "i use a table blue-green-red" so how can i get the entire word without using the (\\w+).(\\w+).(\\w+) n number of times. how can i get that, but there is always a carriage return after the "i use a table blue-green-red\\n" or "i use a table blue\\n" so how can i get the following entire word even if there are n number of dash in the following word

If I understand correctly, what you are trying to extract is the last word (or trailing word ) in the matched search, even if it has dashes. You also indicate that you are guaranteed a newline \\n at the end of the phrase.

With that in mind, a possible solution would be to include a greedy operator right after the word \\w, and curb it with a newline, something like:

regex = r"i use a table (\w+.*)\n"

which matches both:

"i use a table blue\n"

and

"i use a table blue-green-red\n"

extracting the last word.

See it in action here: https://regex101.com/r/34MBP3/1

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