Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove anything between two character

Tags:

java

i want to remove anything between "?" and "/"

my text is "hi?0/hello/hi"

i need to see this out put

"hi?/hello/hi"

My Code Is

key.replaceAll("\\?.*/","?/");

but my Output Is

"hi?/hi"

whats wrong?

like image 564
Ali Kianinejad Avatar asked Mar 09 '15 08:03

Ali Kianinejad


People also ask

How do I remove a string between two characters in Excel?

To eliminate text before a given character, type the character preceded by an asterisk (*char). To remove text after a certain character, type the character followed by an asterisk (char*). To delete a substring between two characters, type an asterisk surrounded by 2 characters (char*char).

How do I extract text between two instances of a character?

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. Note: A1 is the text cell, > and < are the two characters you want to extract string between.

How do I extract text before and after a specific character in Excel?

Extract text before or after space with formula in Excel Select a blank cell, and type this formula =LEFT(A1,(FIND(" ",A1,1)-1)) (A1 is the first cell of the list you want to extract text) , and press Enter button.


2 Answers

You are using greedy matching, so it matches up to the next slash too. Try:

key.replaceAll("\\?.*?/","?/");

An alternative still using greedy matching is to match any character except /:

key.replaceAll("\\?[^/]*/","?/");
like image 189
Andy Turner Avatar answered Sep 29 '22 00:09

Andy Turner


Use this:

key.replaceAll("\\?.*?/","?/")

You can read more about greedyand non greedy matching here

like image 25
Jens Avatar answered Sep 29 '22 00:09

Jens