I am trying to create a REGEX which captures the following string :
returnedData=dfsavdasvfdvdvvjwfwhvfwjhfvwjhevfwjvfw04040000N.sdfsgs.sfgakhvsafjhafj ksajbd 234.234 bfsdf sudhfkusa 77907 23 gfksahgkf bkhkjakjsf - CB123214124
But I want it captured after the numbers in the middle (04040000). Problem is the Regex I am using is capturing it as a whole :
(returnedData)+([A-Za-z0-9=:s-@+?.])*(-s)(CB)
The string I want captured is : N.sdfsgs.sfgakhvsafjhafj ksajbd 234.234 bfsdf sudhfkusa 77907 23 gfksahgkf bkhkjakjsf - CB
This has to be used in IBM LogDNA which does not support Lookbehinds. Hence the limitation. The regex need to be entered in a YAML file so no code is acceptable only REGEX can be used
2
Answers
Use this pattern
If you’re using
JS
you can match string and use the first group as I used inconsole.log()
:The result is:
You can modify your regular expression to use a positive lookbehind assertion, which matches the "- " character sequence only if it is preceded by the digits "04040000". Here is the modified regular expression:
Explanation:
returnedData=
matches the literal string "returnedData="[A-Za-z0-9=:s-@+?.]*?
matches any characters that may appearbefore the target string. The
*?
makes the quantifier lazy, so thatit matches the minimum number of characters necessary to satisfy the
rest of the pattern.
(?<=04040000s- )
is a positive lookbehind assertion that matches the"- " sequence only if it is preceded by "04040000 ".
CBS+
matches the literal string "CB" followed by one or morenon-space characters (the target string).
Note that the
S
character class matches any non-space character. If you want to include spaces in the target string, you can replaceS+
with[A-Za-z0-9s]+
.