Reputation: 107
I have these two sentence
TAGGING ODP:-7.160792, 113.496069
TAGGING pel:-7.160792, 113.496069
I want to match -7.160792 part only if the full sentence contain "odp" in it.
I tried the following (?(?=odp)-\d+.\d+) but it doesn't work, i don't know why.
Any help is appreciated.
Upvotes: 2
Views: 78
Reputation: 163372
The pattern (?(?=odp)\-\d+\.\d+)
is using a conditional (?
stating in the if clause:
If what is directly to the right from the current position is
odp
, then match-\d+.\d+
That can not match.
What you also could do is match odp
followed by any char other than a digit using \D*
and capture the digit part in a group.
\bodp\b\D*(-\d+\.\d+)\b
The pattern matches:
\bodp\b
match odp
between word boundaries to prevent a partial match\D*
Optionally match any char other than a digit(-\d+\.\d+)
Capture -
and 1+ digits with a decimal part in group 1\b
A word boundaryUpvotes: 1
Reputation: 79115
You can use the regex, (?i)(?<=odp:)[^,]*
.
Explanation:
(?i)
: Case-insenstitive flag(?<=odp:)
: Positive lookbehind for odp:
[^,]*
: Anything but ,
👉 If you want the match to be restricted to numbers only, you can use the regex, (?i)(?<=odp:)(?:-\d+.\d+)
Explanation:
(?i)
: Case-insenstitive flag(?<=odp:)
: Positive lookbehind for odp:
(?:
: Start non capturing group
-
: Literal, -
\d+
: 1+ digit(s).\d+
: .
followed by 1+ digit(s))
: End non capturing group👉 If the sign can be either +
or -
, you can use the regex, (?i)(?<=odp:)(?:[+-]\d+.\d+)
Upvotes: 1
Reputation: 18621
(?(?=odp)-\d+\.\d+)
won't work because (?=odp)
is a positive lookahead that imposes a constraint on the pattern on the right, -\d+\.\d+
. Namely, it requires odp
string to occur exactly at the same location where -
and a number are expected.
Use
(?<=ODP:)-\d+\.\d+
ODP:(-\d+\.\d+)
If lookbehinds are supported, the first variant is more viable. Otherwise, another option with capturing groups is good to use.
And if odp
can appear anywhere, even after the number:
(?i)^(?=.*odp).*(-\d+\.\d+)
This will capture the value into a group.
EXPLANATION
--------------------------------------------------------------------------------
(?i) set flags for this block (case-
insensitive) (with ^ and $ matching
normally) (with . not matching \n)
(matching whitespace and # normally)
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
odp 'odp'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
- '-'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \1
Upvotes: 1
Reputation: 65
(?<=ODP:)(-\d+.\d+)
You can try using the negative look behind. This should solve for the code you ve provided.
Upvotes: 0