London
London

Reputation: 15274

Regex in java, group matching

Hello how does java and regex group work. For ex. I want to match any text 'something', the way I'd match this is .+\s+'(.+)'{1}, how can I replace any text 'something' with something?

Meaning replace matched string with 1st matched group.

Upvotes: 2

Views: 528

Answers (1)

jjnguy
jjnguy

Reputation: 138874

If you just want to remove the single quotes, the following will work.

yourString.replaceAll("'([^']+)'", "$1");

That will search for 2 quotes with text in between. And replace it with only the text.

System.out.println("any text 'something'".replaceAll("'([^']+)'", "$1"));

Prints any text something

Upvotes: 6

Related Questions