jlars62
jlars62

Reputation: 7383

Using regex with replaceAll

I have this regex which is used to validate phone numbers.

^\\(?(\\d{2,3})\\)?[-(). ]?(\\d{2,3})[-(). ]?(\\d{4})$

(Yes, I know it is not perfect, but I don't really care). I am just using it to replace phone numbers with another string, say ### to remove sensitive information. So false positives are fine.

It works when the string I am searching is only a phone number. This works:

String PHONE_PATTERN = "^\\(?(\\d{2,3})\\)?[-(). ]?(\\d{2,3})[-(). ]?(\\d{4})$";
String phone = "123-123-1234";
System.out.println(s.replaceAll(PHONE_PATTERN, "###")); //prints '###'

But with surrounding text it does not work:

String PHONE_PATTERN = "^\\(?(\\d{2,3})\\)?[-(). ]?(\\d{2,3})[-(). ]?(\\d{4})$";
String phone = "some other text 123-123-1234";
System.out.println(s.replaceAll(PHONE_PATTERN, "###"));

By does not work, I mean the text is printed unchanged.

What do I need to change on my regex to get this to work so that the second example prints

some other text ###

Upvotes: 2

Views: 82

Answers (3)

hwnd
hwnd

Reputation: 70750

You need to remove the beginning of string ^ and end of string $ anchors, with having both of these set you're matching the entire string from the first character in the string until the last character in the string.

  • The ^ stipulates the pattern must match the substring starting with the first character in the string.
  • The $ stipulates the pattern must match the substring ending with the last character in the string.

If you want to search for a pattern that is at one end or the other, that is when you need to use anchors.

Upvotes: 4

anubhava
anubhava

Reputation: 786091

Instead of anchors ^ and $ use \b (word boundary):

String PHONE_PATTERN = "\\b\\(?(\\d{2,3})\\)?[-(). ]?(\\d{2,3})[-(). ]?(\\d{4})\\b";

Upvotes: 5

Chronio
Chronio

Reputation: 757

Remove the ^ and $ from the beginning and end of your expression. Those characters match the beginning and end of a String, but you don't want the phone number to be the only content of the String, so you should remove them.

Upvotes: 6

Related Questions