4

I need help using a regex.

I have a string with characters and numbers. e.g:

str1="012345m67890man"

I want to keep only the substring "man" and the numbers. I don't need any other alpha characters.

I am using the following Regex, but it's displaying 'm' character also.

str1 = str1.replaceAll("[^(man0-9)]", "");

Actual output: 012345m67890man

Expected output: 01234567890man

If my string has any alpha characters other than man, I need to remove it.

Kindly help me with suggestions.

1 Answer 1

3

You may match and capture man and then just match all characters other than digits and remove them:

str1 = str1.replaceAll("(man)|[^0-9]", "$1");

See the regex demo

IDEONE Java demo:

String str1 = "012345m67890man";
str1 = str1.replaceAll("(man)|[^0-9]", "$1");
System.out.println(str1);

You can read more about Backreferences at regular-expressions.info:

If your regular expression has named or numbered capturing groups, then you can reinsert the text matched by any of those capturing groups in the replacement text. Your replacement text can reference as many groups as you like, and can even reference the same group more than once. This makes it possible to rearrange the text matched by a regular expression in many different ways. As a simple example, the regex \*(\w+)\* matches a single word between asterisks, storing the word in the first (and only) capturing group. The replacement text <b>\1</b> replaces each regex match with the text stored by the capturing group between bold tags.

In Java, we basically use $n backreference syntax, not Perl-like \n.

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

3 Comments

For faster removal use + quantifier with non-digits: "(man)|[^0-9]+"
@WikitorStribizew Thanks a lot wiktor. can you please help me to know the use of $1 in regex with suitable examples.
Please let me know if the details I added are not enough.

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.