Home > Software design >  Return the first occurrence using Regex
Return the first occurrence using Regex

Time:12-29

I have the following expression:

[Document[_id=5f9ecf8ca9bec5549493ba7d,·policy_name=xxx,·is_mobile=false, Document[_id=6090fead53bc363849fce989,·policy_name=yyy,·is_mobile=true, Document[_id=619cf036761c281e3ad12327,·policy_name=zzz,·is_mobile=false, Document[_id=619cf729ea016d1e3336e903,·policy_name=xyz,·is_mobile=false]

I would like to capture ONLY the first Document id (i.e- 5f9ecf8ca9bec5549493ba7d). i tried this regex- (?<=Document\[_id=).*?[^,]* BUT it will return all the Document id's.

1).how can i capture the first / second (Nth match) of document id from the expression?

2). is it possible to do regex AND operator to find the Document id with "is_mobile=true"?
(i.e 5f9ecf8ca9bec5549493ba7d & true)

would really appreciate any help

EDIT: i'm using https://regex101.com/

this is the link in which i tried to capture the first / second (nth occurance of Document id ( i need only the number) - https://regex101.com/r/ZnYRhq/1

CodePudding user response:

There is not language listed, but one approach could be using a capture group for the value that you want, and start the pattern with an anchor ^ to assert the start of the string.

For the first Document id:

^.*?\bDocument\[_id=([^\]\[\s,] )

Regex demo

For the first Document id that has is_mobile=true (assuming that the order of the key-value pairs is as given in the example and is within the same opening and closing square brackets)

^.*?\bDocument\[_id=([^\]\[\s,] ),[^\]\[]*\bis_mobile=true\b

The pattern matches:

  • ^ Start of string
  • .*?\bDocument\[_id= Match the first occurrence of Document[_id=
  • ( Capture group 1
    • [^\]\[\s,] Match 1 times any char except ] [ whitespace char or ,
  • ) Close group 1
  • ,[^\]\[]* Match a comma and optional chars other than ] and [
  • \bis_mobile=true\b Match is_mobile=true between word boundaries

Regex demo

Or using lookarounds for a single (not global) match:

(?<=Document\[_id=)[^,]*(?=,[^][]*\bis_mobile=true\b)

Regex demo

CodePudding user response:

How about this one? (?:^\[Document\[_id=)([^,] ) for the first?

For n-th you need to use capturing group but how to do this is language/framework dependent.

  • Related