Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to remove a substring between two characters

Tags:

java

regex

I want to remove anything between < and > including (< and >) from my string with regular expression. Here are few examples.

Hi<friends>and<family> it should give Hiand

<Rekha Verma>[email protected] then it should give [email protected]

Reva Patel it should give Reva Patel

<Reva Patel>[email protected],<rekha Verma>[email protected] it should give [email protected],[email protected]

Can anyone please give me a regular expression for this? I need to implement it in Java.

like image 623
Ragini Avatar asked Jan 06 '12 13:01

Ragini


People also ask

How do I extract a string between two characters?

Extract part string between two different characters with formulas. To extract part string between two different characters, you can do as this: Select a cell which you will place the result, type this formula =MID(LEFT(A1,FIND(">",A1)-1),FIND("<",A1)+1,LEN(A1)), and press Enter key.


1 Answers

Try using the regex:

<.*?>

For example:

String s = "Hi<friends>and<family>";
System.out.println(s.replaceAll("<.*?>", ""));
like image 61
dogbane Avatar answered Sep 22 '22 04:09

dogbane