Apply regex to catch specific bullets from list with header

54 views Asked by At

I am trying to catch specific list items with regex:

> check listId
> - [v] catch me
> - [x] skip me
> - [ ] catch me too

> ignore thislistId
> - [v] ignore me
> - [x] ignore me also
> - [ ] ignore me too

Expected matched result is

> - [v] catch me
> - [ ] catch me too

This regex works, except like this the regex ignores the listId:

\> - \[(v| )\]\s?[a-z].*\n?

If I precede with a non-capturing group on the listId, the output breaks after the first bullet:

(?:\> check listId\n)(\> - \[(v| )\]\s?[a-z].*\n?)

What is a better approach?

1

There are 1 answers

0
Wiktor Stribiżew On

You can use

/(?<=^> check listId(?:\n> - .*)*\n)> - \[[v ]]\s?[a-z].*\n?/g

See the regex demo.

Details:

  • (?<=^> check listId(?:\n> - .*)*\n)
  • > - \[[v ]] - a > - [v] or > - [ ] substring
  • \s? - n optional whitespace
  • [a-z] - a lowercase ASCII letter
  • .*\n? - the rest of the line and an optional newline char.