java - Using regex with replaceAll -
i have regex used validate phone numbers.
^\\(?(\\d{2,3})\\)?[-(). ]?(\\d{2,3})[-(). ]?(\\d{4})$ (yes, know not perfect, don't care). using replace phone numbers string, ### remove sensitive information. false positives fine.
it works when string searching phone number. 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 surrounding text 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 not work, mean text printed unchanged.
what need alter on regex work sec illustration prints
some other text ###
remove ^ , $ origin , end of expression. characters match origin , end of string, don't want phone number content of string, should remove them.
java regex
No comments:
Post a Comment