0

In an earlier question I asked about sanitizing postal street addresses, one of the respondents recommended this solution:

addressString.replace(/^\s*[0-9]+\s*(?=.*$)/,'');

which is perhaps a valid regex call but is not valid in Java.

I unsuccessfully tried to make this valid Java code by changing it to the following:

addressString.replaceAll("/^\\s*[0-9]+\\s*(?=.*$)/","")

But this code had no effect on the address I tested it with:

310 W 50th Street

Did I not correctly translate this to Java?

Community
  • 1
  • 1
natchy
  • 2,465
  • 2
  • 15
  • 6
  • FYI, you don't need that `(?=.*$)` at the end. @ElRonnoco corrected himself on that point in his answer: http://stackoverflow.com/questions/3636650/how-would-you-sanitize-the-street-number-out-of-a-postal-address-using-java/3636787#3636787 – Alan Moore Sep 03 '10 at 16:07

4 Answers4

5

You don't need the slashes in Java.

addressString.replaceAll("^\\s*[0-9]+\\s*(?=.*$)","")
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
3

You need to take out the slashes at the beginning and end:

addressString.replaceAll("^\\s*[0-9]+\\s*(?=.*$)","")

They're used to quote regexes in some languages, but Java just uses ""

rescdsk
  • 8,739
  • 4
  • 36
  • 32
1

You need to get rid of the forward slashes at the beginning and end.

For the future, you can probably ask for clarification on the answer itself instead of starting a new question. I apologize, I was going to ask the person who gave that answer to translate it to valid Java myself but forgot.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
0

Edited to support street names that start with a number:

Try the following Java String:

"^\\s*[0-9]+\\s*(?=.*$)". 
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Florian
  • 538
  • 4
  • 11