shanky
shanky

Reputation: 801

Find and replace substring beginning and ending with new string java

I want to find and replace a substring beginning with string 'sps.jsp' and ending with substring 'FILE_ARRAY_INDEX=12'.

Following is my string content

beginning with strings............[sps.jsp]..anything between.. [FILE_ARRAY_INDEX=12] ending with some strings....

Below is my code

 Pattern r = Pattern.compile("sps.jsp[\\s\\S]*?FILE_ARRAY_INDEX=12");
 Matcher m = r.matcher(InputStr);
 if (m.find( )) 
 {
   System.out.println("Found value: " + m.group() );
 }

I'm not able to get my pattern and replace it with a new string.

Upvotes: 1

Views: 123

Answers (1)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59978

All you need is to String::replaceAll with this regex sps.jsp(.*?)FILE_ARRAY_INDEX=12

String inputStr = "....";//your input
inputStr = inputStr.replaceAll("sps.jsp(.*?)FILE_ARRAY_INDEX=12", "[some string]");

Outputs

beginning with strings............[some string] ending with some strings....

Upvotes: 1

Related Questions