I want to parse a table line using regex.
Input
|---|---|---|
|---|---|---|
So far I've come up with this regex:
/^(?<indent>\s*)\|(?<cell>- |)/g
Regex101 Link: https://regex101.com/r/wzMYxd/1
But this regex is incomplete.
This only finds the first cell --|
, but I want to find all the following cells as different ----|
.
Question: Can we catch the following cells with the same pattern using the regex? ExpectedOutput: groups with array of matched cells: ["---|", "----|", "---|"]
Note: no constant number of -
is required
CodePudding user response:
How about first checking, if all the line matches the pattern:
^[ \t]*\|(?:- \|) $
See this demo at regex101 - If it matches, extract your stuff:
(?<cell>- )\||^(?<indent>[\t ]*)\|
Another demo at regex101 (explanation on the right side)
With just one regex maybe by sticky flag y
and use of a lookahead for validation.
/^(?<indent>[ \t]*)\|(?=(?:- \|) $)|(?!^)(?<cell>- )\|/gy
The lookahead checks once after the first |
if the rest of the string matches the pattern. If this first match fails, due to the y
flag (matches are "glued" to each other) the rest of the pattern fails too.