Reputation: 1014
I am trying to match a sequence of four numbers that are separated by pipes in a string. The numbers may be negative, float, or double digits, for example:
13|5|-1|3
or 5|5|0|3
or 13|4|1.5|1
The string may also contain additional numbers and words; a full example looks like so:
SOME STRING CONTENT 13|5|-1|3 MORE 1.6 CONTENT HERE
How could I identify those numbers between and to the left/right of the pipes using regex?
I have tried [\d\-.\|]
which matches all digits, decimals, pipes, and negative signs but also find it matches the additional number/decimal content in the string. Any help on just selecting that one section would be appreciated!
Upvotes: 1
Views: 157
Reputation: 18641
As well use
(?<!\S)-?\d*\.?\d+(?:\|-?\d*\.?\d+){3}(?!\S)
See proof.
EXPLANATION
--------------------------------------------------------------------------------
(?<! look behind to see if there is not:
--------------------------------------------------------------------------------
\S non-whitespace (all but \n, \r, \t, \f,
and " ")
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
-? '-' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\.? '.' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (3 times):
--------------------------------------------------------------------------------
\| '|'
--------------------------------------------------------------------------------
-? '-' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\.? '.' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
){3} end of grouping
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\S non-whitespace (all but \n, \r, \t, \f,
and " ")
--------------------------------------------------------------------------------
) end of look-ahead
Upvotes: 0
Reputation: 163577
You can use
-?\b\d+(?:\.\d+)?(?:\|\-?\d+(?:\.\d+)?){3}\b
The pattern matches:
-?
Match an optional -
\b
A word boundary to prevent a partial match\d+(?:\.\d+)?
Match 1+ digits with an optional decimal part(?:\|\-?\d+(?:\.\d+)?){3}
Repeat 3 times the same as previous part preceded by a pipe\b
A word boundaryUpvotes: 1