1

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

  • Like this right? With a single capture group: `returnedData.*?04040000([A-Za-z0-9=:\s@+?.-]+-\sCB)` https://regex101.com/r/0zsBkB/1 – The fourth bird May 12 '23 at 10:27
  • It is capturing as a group but I want it as match because in the given regex the whole string is getting matched. Need some solution like positive lookbehind but I have a limitation that I can't use positive look behind. Thanks for the help though – Anubhav Chandraul May 12 '23 at 10:35
  • 1
    With a positive lookbehind in javascript `(?<=returnedData.*?04040000)[A-Za-z0-9=:\s@+?.-]+-\sCB` See https://regex101.com/r/IAvD7j/1 – The fourth bird May 12 '23 at 10:36
  • @Thefourthbird can you add an answer rather than answering in comments? – Liam May 12 '23 at 10:37
  • The Positive lookbehind solution you provided is the exact desired outcome but that's the limitation of the environment that positive look behind is not acceptable there. Hence need something with the same same but without lookbehind. – Anubhav Chandraul May 12 '23 at 10:43
  • @AnubhavChandraul Can you update the question with how or where you are using this pattern. Are you using any code? – The fourth bird May 12 '23 at 10:43
  • @mplungjan [there's been a pretty long standing discouragement of answering in comments](https://meta.stackoverflow.com/questions/253045/answerers-who-only-use-comments). If this question isn't worth answering then it should be closed – Liam May 12 '23 at 10:45
  • @Liam However the very useful "Too localised" close reason was removed. Also as we can see, it is not clear yet what is needed – mplungjan May 12 '23 at 10:48
  • @mplungjan I tried to find a good dupe and couldn't, so this seems like a legitimate question IMO – Liam May 12 '23 at 10:50
  • From your meta: _Discussion in comments, and rightly placed there, does eventually elicit the source of the problem. The commenter who solved the problem (or even someone else) then posts the answer as an answer. The comments show that the problem was trivial. I usually vote to close, the closest match reason being "it's a typo or no longer an issue"._ – mplungjan May 12 '23 at 10:51
  • @Thefourthbird I have updated the question with the environment where it needs to be used. – Anubhav Chandraul May 12 '23 at 10:52
  • @Liam Bingo. So The fourth bird rightly refrained from posting their lookbehind as answer – mplungjan May 12 '23 at 10:53
  • @AnubhavChandraul You updated the question, but how are you using this? Can you use any JavaScript code (as you tagged this) to get the capture group value? Or are you using a system where you can only enter a regex that only gives back a match? – The fourth bird May 12 '23 at 11:02
  • This will match it `\D*\s- CB` https://regex101.com/r/ynReiO/1 – The fourth bird May 12 '23 at 11:22
  • @Thefourthbird The regex need to be entered in a YAML file so no code is acceptable only REGEX can be used. The solution you provide above will only work if there is no digit in the response body. Please find the updated question. – Anubhav Chandraul May 12 '23 at 11:36
  • Maybe you can exclude 8 digits or the full number `\D(?:(?!04040000).)*?\s- CB` https://regex101.com/r/6P5kFg/1 – The fourth bird May 12 '23 at 11:52
  • Does the OP want to **capture** or does the OP want to **match**? There are big differences. – Peter Seliger May 12 '23 at 16:31
  • 1
    Why is this tagged with `javascript` when apparently JavaScript capabilities cannot be used? – trincot May 13 '23 at 13:28

1 Answers1

0

Use this pattern

returnedData\s*=\s*\D+\d++([A-Za-z0-9=:\s\-@+?\.]+\s*-\s*CB)

If you're using JS you can match string and use the first group as I used in console.log():

str="returnedData=dfsavdasvfdvdvvjwfwhvfwjhfvwjhevfwjvfw04040000N.sdfsgs.sfgakhvsafjhafj  ksajbdfksabfkasbfsdf  sudhfkusagfksahgkf bkhkjakjsf - CB123214124"
let matched = str.match(/returnedData\s*=\s*\D+\d+(\D[A-Za-z0-9=:\s\-@+?\.]+\s*-\s*CB)/);
console.log(matched[1]);

The result is:

N.sdfsgs.sfgakhvsafjhafj ksajbdfksabfkasbfsdf sudhfkusagfksahgkf bkhkjakjsf - CB

Alijvhr
  • 1,695
  • 1
  • 3
  • 22
  • 1
    Regex works fine to identify the whole string but defeats the requirement specified in the question. Thanks though :-) – Anubhav Chandraul May 12 '23 at 11:52
  • @AnubhavChandraul It works fine. You should select the first part of extraction... Im js works. If you use other language, tell me to rewrite the code... If you run the snippet you will see the result here... – Alijvhr May 12 '23 at 13:17
  • @AnubhavChandraul you should just use first group! Thats why i used `[1]` in code! – Alijvhr May 12 '23 at 13:18