Why does this Java regular expression work inconsistently to remove street numbers from US street addresses?

I'm trying to cross out a street number from a mailing address.

I have a regex in Java:

address.replace("^\\s*[0-9]+\\s+","");

It works at this address:

301 West 23rd Street

doing this:

West 23rd Street

But when I apply it to this address, the address does not change:

70-50 69th Place

Instead, it should be:

69th Place

Any ideas?

+3
source share
4 answers

Your regular expression does not match this string. Here is a regular expression explanation

^ Start of string. Matches successfully.
\\ s * Zero or more whitespace. Matches the empty string.
[0-9] + One or more digits. Matches "70".
\\ s + One or more whitespace. Fails to match.

"70" - , , . , :

address = address.replace("^\\s*[0-9-]+\\s+", "");

, ( ), :

  • ( , Java ).
+4

, . . , -s, :

address.replace("^\\s*([0-9-]+\\s+)+","");
+1

, : , , , .

"" , , , , , .

, : "^\\s*[0-9-]+\\s+"

+1

... , , Mt. . , , .

, , , ? "th" "st" .. .. ( ).

SmartyStreets, - . CASS API, , ( ), , , . - LiveAddress, . , , .

0

Source: https://habr.com/ru/post/1764572/


All Articles