d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Someone Help Me With String.split(regex)
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 12 2014 03:02pm
I am trying to grab the temperature of St. Louis from

http://www.nws.noaa.gov/view/prodsByState.php?state=MO&prodtype=hourly

the line I get specifically is

Code
ST. LOUIS INTL PTSUNNY 35 17 47 N10G18 30.36F WCI 27


My code originally was something like

Code
info = crtLine.split("\\s");


what I expected:

Code
ST.
LOUIS
INTL
PTSUNNY
35
17
47
N10G18
30.36F
WCI
27


however I get an array where several of the elements are whitespace

What I need is that 35, the first number that appears.

I am not the best with regex.

I have a feeling I am missing something here.

Any java folks know what is up?

edit: so I have been testing with rubular

and if I do

Code
([0-9]+)\s


It seems to correctly return elements where the first element is the number I want, so I would imagine I could do str[0] and access it.

But instead I get

Code
ST.
LOUIS
INTL
PTSUNNY
(as one line)

This post was edited by Eep on Nov 12 2014 03:15pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 12 2014 03:16pm
Try crtLine.split(\\s+)
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 12 2014 03:19pm
Quote (Minkomonster @ Nov 12 2014 04:16pm)
Try crtLine.split(\\s+)


thank you, this worked.

Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 12 2014 03:20pm
Quote (Eep @ Nov 12 2014 04:19pm)
thank you, this worked.


Love you bae


Do you understand why this works and the other doesn't? That should 'be the take away

This post was edited by Minkomonster on Nov 12 2014 03:20pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Nov 12 2014 03:28pm
Quote (Minkomonster @ Nov 12 2014 04:20pm)
Love you bae


Do you understand why this works and the other doesn't? That should 'be the take away


I think. it seems the .split() treats the regex quite literally?

so like, the way the string is formatted from the weather guys, there are several spaces between the PTSUNNY and the 35.

so I am guessing that for [_ _ _]

the /s would still capture

[_x_] where x is that white space in the middle. (because I only specified it to delimit on a single white space)

edit:

actually, it would catch

[xx_]

I believe

double edit:

I am still wrong.

There are 4 spaces between
Code
N10G18 30.36F


the array shows 6 elements as white space

triple edit:

apparently there are 7 spaces, so 6 makes sense then (in the java console anyway it sees 6, I got 4 from jsp)

This post was edited by Eep on Nov 12 2014 03:35pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 12 2014 03:42pm
Basically yes. The engine is splitting on each instance of the delimiter, in this case any whitespace. But because your input text contains consecutive whitespace, when it splits it will return empty characters.

The difference is that +, it tells it 1 or more . So now it will group all consecutive whitespace as a single delimiter and results in no whitespace being generated.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll