How can I ignore the first x number of characters in a string when matching with a Regex (without using a look behind)?

131 Views Asked by At

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

1

There are 1 best solutions below

3
On

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