2

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.

6
  • 2
    Just FYI, your {1} is redundant -- {1} is implied on every atom. Commented Dec 13, 2010 at 14:59
  • 1
    please give a more specific example, e.g. exact input ABCXYZ, exact desired output ABC123 or whatever. Commented Dec 13, 2010 at 15:00
  • For regular expressions, I always go check on the interwebz, on sites like fileformat.info/tool/regex.htm Commented Dec 13, 2010 at 15:05
  • @cdhowie, the {1} is also only checking for a single ' which is not likely what the OP intended. Commented Dec 13, 2010 at 15:07
  • @Brad: But the ' already does exactly that. ' is equivalent to '{1}. And {1} applied anywhere on a regex is meaningless and redundant, since it will never modify any regex's behavior. Commented Dec 13, 2010 at 15:08

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.