hello. I want to select all the words that end with ! from tag:
<title>I come home! at night on ten and I got</title>
In this case, I want to match the word _home!_
from <title>
tag
I try this regex, but doesn’t work: (?s)<title>\w+!</title>
hello. I want to select all the words that end with ! from tag:
<title>I come home! at night on ten and I got</title>
In this case, I want to match the word _home!_
from <title>
tag
I try this regex, but doesn’t work: (?s)<title>\w+!</title>
but. another question. If I want to replace all words like home! with home ! (add a space before “!” ), how can I use these regex?
The regex (\w+)!
would match all words followed by an exclamation point, at which point the replacement text would be \1 !
to replace with the captured word, a space, and a new exclamation point.
I’ll leave it as an exercise to the reader how you would go about adding that to the regex that you outlined above.
The best thing to do is read up on your chosen regex engine’s syntax and practice your regex fu. Unfortunately the more and more complex stuff you want to do, the less people have time to construct regex for you, unless they really like that kind of thing (there are some people like that out there). Me personally, I just research and experiment.
The big things to be aware of is capturing groups can return specific groups in replace via \1
etc. Use those to your advantage if you need to reuse text you find. But be aware that most regular expression engines only keep the last capture of a group, so things like (pattern)*
will only yield the last instance of that match, not all *
instances of them (there are some engines that can capture this, but I won’t go into that here).
In the end not everything is workable with pure regex only, but you can do a lot as you learn more. Read up and practice is the best advice.
or, another sugestion:
Search: (?:\G(?!^)|<title>)[^<]*?\K\b!\B
Replace by: (empty space)!