Skip to content

How to get regex submatches? (using --json) #2814

Answered by BurntSushi
bew asked this question in Q&A
Discussion options

You must be logged in to vote

OK, so the issue here is that submatches is not the same as capture group matches. submatches in this context refers to the list of matches within a line. For example:

$ echo 'foo bar baz' | rg --json '\w+' | rg '"match"'
{"type":"match","data":{"path":{"text":"<stdin>"},"lines":{"text":"foo bar baz\n"},"line_number":1,"absolute_offset":0,"submatches":[{"match":{"text":"foo"},"start":0,"end":3},{"match":{"text":"bar"},"start":4,"end":7},{"match":{"text":"baz"},"start":8,"end":11}]}}

Otherwise, I think you're probably looking for #2325. The main issue is that adding capture groups will result in a perf hit. So it means that you have to opt into it. Which is kinda unfortunate.

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@bew
Comment options

Comment options

You must be logged in to vote
1 reply
@bew
Comment options

Answer selected by bew
Comment options

You must be logged in to vote
1 reply
@BurntSushi
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants