Reputation: 29
I want to replace each occurence of a character only between two specific characters within a text.
For example, here I want to replace each occurence of whitespaces or umlauts only between two quotation marks; also for multiple links:
1. Here is a <a href="#foö bar foo bär">link</a> for foö bar and foo bär.
2. Here is a <a href="#foö bar foo bär">link</a> for foö bar and foo bär.
3. Here is a <a href="#foö bar foo bär">link</a> for foö bar and foo bär.
So I want to get the following text:
1. Here is a <a href="#fooe_bar_foo_baer">link</a> for foö bar and foo bär.
2. Here is a <a href="#fooe_bar_foo_baer">link</a> for foö bar and foo bär.
3. Here is a <a href="#fooe_bar_foo_baer">link</a> for foö bar and foo bär.
I was thinking about replacing something like this:
(?<="[^"]*)\(s+?)(?=[^"]*")|(?<="[^"]*)(ä)(?=[^"]*")|...
with:
(?1_)(?2ae)...
But quantifiers in a lookbehind doesn't work. Thanks in advance...
Upvotes: 2
Views: 294
Reputation: 625
This should do the trick
as 'search' you take:
"(.*)((ä)|(ö)|(ü)|(ß)|(\s))(.*)"
as 'replace' you take
"\1(?2(?3ae)(?4oe)(?5ue)(?6sz)(?7_))\8"
Edit: added the missing umlauts
Upvotes: 0
Reputation: 67988
(\s+)(?![^"]*(?:"[^"]*")*[^"]*$)
You use this with lookahead.See demo.
https://regex101.com/r/JLANJC/1
Upvotes: 1