skip to Main Content

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


  1. Use this pattern

    returnedDatas*=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(/returnedDatas*=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

    Login or Signup to reply.
  2. 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:

    returnedData=[A-Za-z0-9=:s-@+?.]*?(?<=04040000s- )CBS+
    

    Explanation:

    • returnedData= matches the literal string "returnedData="
    • [A-Za-z0-9=:s-@+?.]*? matches any characters that may appear
      before the target string. The *? makes the quantifier lazy, so that
      it 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 more
      non-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 replace S+ with [A-Za-z0-9s]+.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search