Home > Blockchain >  regex capturing multiple group after a specific string
regex capturing multiple group after a specific string

Time:02-16

I want to capture all variable names after "define" keyword (im using js).

I already tried this:

(?<=define\s)(?:([a-zA-Z_]\w*),?\s?) 

but it only captures last occurrence, but i want to get all of them. example string :

define x, y, z, a

link here regex101

CodePudding user response:

If you are using a browser that supports an infinite quantifier in the lookbehind assertion:

(?<=define\s[\w,\s]*)[a-zA-Z_]\w*

The pattern matches:

  • (?<= Positive lookbehind, assert what is at the left is
    • define\s Match define followed by a whitspace char
    • [\w,\s]* Match optional allowed chars in between
  • ) Close lookbehind
  • [a-zA-Z_]\w* Match a single char a-zA-Z or _ and optional word chars

Regex demo

const s = "define x, y, z, a";
const regex = /(?<=define\s[\w,\s]*)[a-zA-Z_]\w*/g;
console.log(s.match(regex));

CodePudding user response:

Another way round, extract all and split:

const s = `define x, y, z, a`
const m = s.match(/define\s ((?:[a-zA-Z_]\w*,?\s?) )/)
console.log(m[1].split(', '))

Results: ['x', 'y', 'z', 'a']

EXPLANATION

--------------------------------------------------------------------------------
  define                   'define'
--------------------------------------------------------------------------------
  \s                       whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (1 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      [a-zA-Z_]                any character of: 'a' to 'z', 'A' to
                               'Z', '_'
--------------------------------------------------------------------------------
      \w*                      word characters (a-z, A-Z, 0-9, _) (0
                               or more times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      ,?                       ',' (optional (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      \s?                      whitespace (\n, \r, \t, \f, and " ")
                               (optional (matching the most amount
                               possible))
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
  )                        end of \1
  • Related