regex help

  • Thread starter Thread starter Tem
  • Start date Start date
T

Tem

I need to write a regex pattern that matches (somethingA|somethingB) and
replace it with text on the left side of |

Input
"(something) something (somethingA|somethingB) something
(somethingC|somethingD) (something) something"

Output
"(something) something (somethingA) something (somethingC) (something)
something"

Thanks

Tem
 
Tem said:
I need to write a regex pattern that matches (somethingA|somethingB)
and replace it with text on the left side of |

Input
"(something) something (somethingA|somethingB) something
(somethingC|somethingD) (something) something"

Output
"(something) something (somethingA) something (somethingC) (something)
something"

Try:

string s = "(something) something (somethingA|somethingB) something
(somethingC|somethingD) (something) something";
string s2 = Regex.Replace(s, @"(\([^\|\)]*)(\|[^\)]*)(\))", "$1$3");

Arne
 
thanks arne

Arne Vajhøj said:
Tem said:
I need to write a regex pattern that matches (somethingA|somethingB) and
replace it with text on the left side of |

Input
"(something) something (somethingA|somethingB) something
(somethingC|somethingD) (something) something"

Output
"(something) something (somethingA) something (somethingC) (something)
something"

Try:

string s = "(something) something (somethingA|somethingB) something
(somethingC|somethingD) (something) something";
string s2 = Regex.Replace(s, @"(\([^\|\)]*)(\|[^\)]*)(\))", "$1$3");

Arne
 
How can I modify this so that the replace string does have ()

Input
"(something) something (somethingA|somethingB) something
(somethingC|somethingD) (something) something"

Output
"(something) something somethingA something somethingC (something)
something"


I tried to do it myself, but couldn't get it to work.


Arne Vajhøj said:
Tem said:
I need to write a regex pattern that matches (somethingA|somethingB) and
replace it with text on the left side of |

Input
"(something) something (somethingA|somethingB) something
(somethingC|somethingD) (something) something"

Output
"(something) something (somethingA) something (somethingC) (something)
something"

Try:

string s = "(something) something (somethingA|somethingB) something
(somethingC|somethingD) (something) something";
string s2 = Regex.Replace(s, @"(\([^\|\)]*)(\|[^\)]*)(\))", "$1$3");

Arne
 
I got it. missed a \

Tem said:
How can I modify this so that the replace string does have ()

Input
"(something) something (somethingA|somethingB) something
(somethingC|somethingD) (something) something"

Output
"(something) something somethingA something somethingC (something)
something"


I tried to do it myself, but couldn't get it to work.


Arne Vajhøj said:
Tem said:
I need to write a regex pattern that matches (somethingA|somethingB)
and replace it with text on the left side of |

Input
"(something) something (somethingA|somethingB) something
(somethingC|somethingD) (something) something"

Output
"(something) something (somethingA) something (somethingC) (something)
something"

Try:

string s = "(something) something (somethingA|somethingB) something
(somethingC|somethingD) (something) something";
string s2 = Regex.Replace(s, @"(\([^\|\)]*)(\|[^\)]*)(\))", "$1$3");

Arne
 
Tem said:
How can I modify this so that the replace string does have ()

Input
"(something) something (somethingA|somethingB) something
(somethingC|somethingD) (something) something"

Output
"(something) something somethingA something somethingC (something)
something"

I tried to do it myself, but couldn't get it to work.

You asked that question a couple of weeks ago and I
replied back then.

Arne
 
Back
Top