Skip to content
Advertisement

Regex capture first text group within quotes per line

I’m working on writing a simple highlighter and I need to capture the all the text including the quotes, for the first word per line. How can I adjust this to do so? Currently this gets me every group of words within quotes, however i need just the first one.

Here are two regex i’ve found capture words within quotes ("[^"]*") (".*?[^\]")

enter image description here

I’m just tryin to make a simple json syntax highlighter in pyside.

JavaScript

Similar question…how do i capture numbers without the trailing comma? (d+),

JavaScript

Advertisement

Answer

Use a capture group and return it:

JavaScript

See regex proof.

EXPLANATION

NODE EXPLANATION
^ the beginning of the string
[ t]* any character of: ‘ ‘, ‘t’ (tab) (0 or more times (matching the most amount possible))
( group and capture to 1:
" ‘”‘
[^"]* any character except: ‘”‘ (0 or more times (matching the most amount possible))
" ‘”‘
) end of 1
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement