-2

I am not great with Regex and I am trying to replace two characters in a set string.

I need this:

This is some variable text
<foo> some stuff <Hello World> other stuff <bar>

To be replaced with this:

This is some variable text
<foo> some stuff (Hello World) other stuff <bar>

Edit: In this example I want to find all < and >, to replace them with ( and ) respectively between <foo> and <bar> in the larger string.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • 2
    What makes `Hello World` special? Why shouldn't `foo` and `bar` also be replaced? – Nick Dec 17 '19 at 23:30
  • Hello world is not special, foo and bar are special because I would like to only target the "<" and ">" in between those tags. Hello world must simply stay the same. – Tyler Lawson Dec 17 '19 at 23:50
  • 1
    Please always add environment/tool you're using and what regex you already wrote but did not work. If this is for JS, have a try with something [like this demo](https://tio.run/##lY2xDoIwEEB3vuIGE1qpIDgCdfUPXKlwgKblSFv9/dpgjLPLDXfv3Xuol3K9va/@sNCAIWj04LyFFrpmJJLgyGDcPMcRmgtqTXAlqwcJ5Ge038tNWQn/Cl2dJJ9WnLnFVaseWcG2P5zl@zNnG8iLSQAzR2FKYSphThxaCabMTPXTmk2QEU3ZruQpzyIXCz0tjjTmmiYWO7wO4Q0). – bobble bubble Dec 18 '19 at 00:18
  • @bobblebubble Thank you! My apologies for not specifying the environment, but your demo solves my problem! – Tyler Lawson Dec 18 '19 at 00:22
  • @TylerLawson You're welcome glad it helped :) I posted an answer. – bobble bubble Dec 18 '19 at 00:35

4 Answers4

2

Use this command with the regex as follows:

perl -pe 's/(<foo>.*)<(.*)>(.*<bar>)/$1($2)$3/'
jkdev
  • 11,360
  • 15
  • 54
  • 77
Vesa Karjalainen
  • 1,087
  • 8
  • 15
2

How about using two replaces. One for (<foo>)(.*?)(<bar>) and on what was captured by the second capturing group a callback function that replaces <(.*?)> with ($1)

s = s.replace(/(<foo>)(.*?)(<bar>)/g, (m0,m1,m2,m3) => m1+m2.replace(/<(.*?)>/g,'($1)')+m3);

See this JS demo at tio.run

For multiline strings use ([\s\S]*?) instead of (.*?) to skip over newlines (the dot, read more).

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • Hi @bobblebubble I have another project I need this same problem solved written in ASP.NET. Is it possible to get this same solution done with .NET/C# ? – Tyler Lawson Dec 19 '19 at 23:54
  • 1
    @TylerLawson Probably you can do this in .NET in a similar way. Something [like this demo](https://tio.run/##XZA/T8MwEMXn@FOcIoYYSqTCmBCGCsFAF4joUBgc99JYOHHkc6qgqp89OOkfISwvfvfe0@8s6VYai8PQkWq28P5DDuuE/X3FOfYufsNtp4V96luLRMo0lDAmtSCCHMmxPQvICack7IzawFKoJuIs8HKwExYIHiDMK0XgL5kawatKFBrB@frPJi2NyY4Tcl1ZQvqCWhtYGas3GRhXoT1PCmGzMPHNY6sHw97jtVpIjGgGYTR18Si@fuTRZObhDBZC60LIbz4GF57faIxXVjl8VY0PjvqBXbYgZ8cvOKeipXCygprDnoE/QWDRdbaBOn62pmtpPf@KP4TuEG7@IV0cdyeHR0wnuCwcaa/mPOQ@dfHdn3xHoMMw/AI). I have no experience in .NET, you might just want to ask another question. – bobble bubble Dec 20 '19 at 09:17
0

console.log(`This is some variable text
<foo> some stuff <Hello World> other stuff <bar>`
.replace(/<\w+>|([<>])/g, (m, c) => ({'<':'(', '>':')'}[c] || m)))
Qwertiy
  • 19,681
  • 15
  • 61
  • 128
0

console.log(`This is some variable text
<foo> some stuff <Hello World> other stuff <bar>`
.replace(/<(?!\w+>)/g, "(")
.replace(/>(?<!<\w+>)/g, ")")
)
Qwertiy
  • 19,681
  • 15
  • 61
  • 128
  • Lookbehind doesn't works in all browsers, with Firefox I get `"message": "SyntaxError: invalid regexp group",`. This will do the replacement even outside `...` – Toto Dec 18 '19 at 11:02
  • The answer was posted for [original version of question](https://stackoverflow.com/revisions/59383531/1). – Qwertiy Dec 18 '19 at 11:14
  • @Toto, there was no [tag:javascript] tag on the question. By the way, this tag is added by other user. I expect asker to know which regex engine he uses and what features are supported by it. – Qwertiy Dec 18 '19 at 11:15