Gowri Sundar
Gowri Sundar

Reputation: 719

Java Regex to use regex to keep expected string and remove others

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.

Upvotes: 4

Views: 2226

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

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.

Upvotes: 3

Related Questions