I have been attempting to get a regex lookahead conditional to work, but am having some trouble. I've consulted the forums, to no avail.
I am parsing some data where a field can contain one or more numbers; I want to use regex to create a conditional whereby if the field contains any whitespace then the last number is returned, else if the cell value does not contain any whitespace the whole field is returned.
Example field:
6,300.69 22,359.06 11,712.20 40,371.95 0.00 0.00 0.00 40,371.95
Example regex: /(?(?=\s)\s+\S*$|.*)/
So again, IF "\s" is found, THEN apply "\s+\S*$", ELSE apply ".*"
Individually, each regex returns what I would expect.
\s returns the first whitespace.
\s+\S*$ returns any non-whitespace characters after the final whitespace.
.* returns the whole field
My syntax appears correct according to regex101.com and regular-expressions.info/conditional.html.
Any idea what I'm doing wrong?
My regex /(?(?=\s)\s+\S*$|.*)/ returns the following right now:
Example field 1:
6,300.69 22,359.06 11,712.20 40,371.95 0.00 0.00 0.00 40,371.95
Returns:
6,300.69 22,359.06 11,712.20 40,371.95 0.00 0.00 0.00 40,371.95
Example field 2:
40,371.95
Returns:
40,371.95
Here is a Java solution. This works for both Strings
s1ands2. The main difference is that I am using a look-behind.(?<=\\s)prints
For non-Java use, the regex would be: