1

I got this regex

(?<=token=)(.*?)(?=;)

and on the https://regex101.com it says that

? The preceding token is not quantifiable

I'm trying to get string after token= excluding it and before first ; excluding it too

string = [ 'nginx/1.15.2', 'Fri, 22 Feb 2019 22:39:19 GMT', 'application/json', '76', 'close', [ 'token=fCMNSX6y85W.7jOzwvpp8GQ; Secure; HttpOnly; expires=Wed, 21 Oct 2099 04:24:00 GMT' ], 'content-type,cache-control,pragma,x-request-id', 'true' ] '

if I'm using this regex token=(.*?)(?=;)

I'm getting token=fCMNSX6y85W.7jOzwvpp8GQ

so, all I need from this string is fCMNSX6y85W.7jOzwvpp8GQ

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
antvish
  • 21
  • 4
  • Just split the result with `=` and take the first index. lookbehinds is included in JS recently so it is not available on all platform till now – Code Maniac Feb 23 '19 at 07:14
  • If you want to use regexp - just use 'token=(.*?);' https://regex101.com/r/Ng8jRS/1 to get first match. – Daniil Feb 23 '19 at 07:17
  • With pattern `token=(.*?)(?=;)` your value is in the first capturing group. You may also try `token=([^;]+)` and your value is in the first capturing group as well https://regex101.com/r/X6RrWq/1. – The fourth bird Feb 23 '19 at 07:19

3 Answers3

1

Your value in the pattern token=(.*?)(?=;) is in the first capturing group.

You could simplify that pattern to token=([^;]+) capturing in a group matching not a semicolon.

regex101 demo

var s = `string = [ 'nginx/1.15.2',
  'Fri, 22 Feb 2019 22:39:19 GMT',
  'application/json',
  '76',
  'close',
  [ 'token=fCMNSX6y85W.7jOzwvpp8GQ; Secure; HttpOnly; expires=Wed, 21 Oct 2099 04:24:00 GMT' ],
  'content-type,cache-control,pragma,x-request-id',
  'true' ] '`;

var pattern = /token=([^;]+)/;
console.log(s.match(pattern)[1]);
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

You are almost there

token=(.*)(?=;) will capture the token in the capturing group (.*). Depending on which command/scripting language you use you can use a backreference to get the capturing group value.

If you're using JS it'll be something like this

var re = /token=(.*)(?=;)/
var found = 'token=fCMNSX6y85W.7jOzwvpp8GQ; Secure'.match(re);
if (found) {
    console.log(found[1])
}

Note found[1] is the first capturing group of the match.

https://jsfiddle.net/ogj4xpLc/2/

ubi
  • 4,041
  • 3
  • 33
  • 50
0

Step 1

Find

^string.+\n.+\n.+\n.+\n.+\n(.+)\n.+\n.+

Replace

$1

Step2

Find

.*token=(.*?);.*

Replace

$1\n
Joseph
  • 80
  • 14