-1

I'm trying to capture the bellow parts from the attached log:

aff_lsr?tt_adv_id=806&tt_cid=&tt_adv_sub=b3fff3722fc6b52aedde9b86bb22bf23&tt_time=2016-04-05+16%3A08%3A18&

should capture:

●   tt_adv_id
●   806
●   tt_adv_sub 
●   b3fff3722fc6b52aedde9b86bb22bf23
●   tt_time
●   2016-04-05+16%3A08%3A18

I have tried to create a regex to extract all strings which start with either “?” “&” or “=” and end with either “=” or “&”

this is the regex I have tried:

(?=[?/&/=]).*?(=)|(?=[=/&])

it ignores all parts between “=” and “&”

so the result i get is :

?tt_adv_id =

&tt_cid=

&tt_adv_sub=

&tt_time =
Emma
  • 27,428
  • 11
  • 44
  • 69
Dror Yelin
  • 11
  • 3

2 Answers2

0

Maybe, these simple expressions would extract our desired values:

.*(tt_adv_id)=([^&]*)&(?:tt_cid)=(?:[^&]*)&(tt_adv_sub)=([^&]*)&(tt_time)=([^&]*).*

or

(tt_adv_id)=([^&]*)&(?:tt_cid)=(?:[^&]*)&(tt_adv_sub)=([^&]*)&(tt_time)=([^&]*)

The expression is explained on the top right panel of this demo, if you wish to explore further or modify it, and in this link, you can watch how it would match against some sample inputs step by step, if you like.

const regex = /.*(tt_adv_id)=([^&]*)&(?:tt_cid)=(?:[^&]*)&(tt_adv_sub)=([^&]*)&(tt_time)=([^&]*).*/gm;
const str = `aff_lsr?tt_adv_id=806&tt_cid=&tt_adv_sub=b3fff3722fc6b52aedde9b86bb22bf23&tt_time=2016-04-05+16%3A08%3A18&
`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}
Emma
  • 27,428
  • 11
  • 44
  • 69
0

You can simply use

/[?&=]([^&=]+)/
  • [?&=] - Match ?, & or =
  • ([^&=]+) - Match anything except & and = one or more time

enter image description here

and than drop off the first character of each match,

let str = "aff_lsr?tt_adv_id=806&tt_cid=&tt_adv_sub=b3fff3722fc6b52aedde9b86bb22bf23&tt_time=2016-04-05+16%3A08%3A18&"

let matched = str.match(/[?&=]([^&=]+)/g).map((v)=>v.substr(1))

console.log(matched)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60