siddhver February 2016

Merge multiple regex in Java

I have written a regex to omit the characters after the first occurrence of some characters (, and #)

String number = "(123) (456) (7890)#123";
number = number.replaceAll("[,#](.*)", ""); //This is the 1st regex

Then a second regex to get only numbers (remove spaces and other non numeric characters)

number = number.replaceAll("[^0-9]+", ""); //This is the 2nd regex

Output: 1234567890

How can I merge the two regex into one like piping the O/p from first regex to the second.

Answers


Tapan Prakash February 2016

You can combine both regex in the following way.

String number = "(123) (456) (7890)#123";
number = number.replaceAll("[,#](.*)", "").replaceAll("[^0-9]+", "");


Wiktor Stribiżew February 2016

So you need to remove all symbols other than digits and the whole rest of the string after the first hash symbol or a comma.

You cannot just concatenate the patterns with |operator because one of the patterns is anchored implicitly at the end of the string.

You need to remove any symbols but digits AND hashes with commas first since the tegex engine processes the string from left to right and then you can add the alternative to match a comma or hash with any text after them. Use DOTALL modifier in case you have newline symbols in your input.

Use

 (?s)[,#].*$|[^#,0-9]+

Post Status

Asked in February 2016
Viewed 2,855 times
Voted 14
Answered 2 times

Search




Leave an answer