2

I've got the below sample text

Hello | World
<Hi | Hello|How | are | you><test|string |for |regex>
sample | text <however|replace|pipe>

to be converted as below

Hello | World
<Hi ~ Hello~How ~ are ~ you><test~string ~for ~regex>
sample | text <however~replace~pipe>

i.e. Replace | within <> with ~

I tried this <(?:.*?)(\|)(?:.*?)> (http://regex101.com/r/mX1sO0)

But it matches only the first | withing the angle <>. And I am not sure how to replace it. Any directions?

4

1 回答 1

1

If your angle brackets are never nested and always correctly balanced, then you can do it:

\|(?=[^<>]*>)

matches only those pipe characters where the next angle bracket is a closing angle bracket. Then just replace the matches with ~.

See it live on regex101.com.

于 2013-12-08T08:49:36.313 回答