Search code examples
javascriptregexsource-engine

Regex Match string after keyword between brackets


I need to match value after keyword between double quote for example:

zoom_sensitivity "2"
sensitivity "99"
m_rawinput "0"
m_righthand "0"

also with different spacing:

sensitivity"99"m_rawinput"0"zoom_sensitivity"2"m_righthand"0"

another example:

sensitivity"99" m_rawinput "0"
m_righthand "0"
zoom_sensitivity"2"

i want to get 99 value in both scenarios after sensitivity keyword or chosen one

What i tried is: [\n\r]*["|\n\r\s]sensitivity\s*"([^\n\r\s]*)"

but it does not match if the keyword is in the first line or before any whitespace/double quote and with inline code it match more than just 99 value. I believe Source Engine parse it similar from their .cfg files and maybe there is better way.


Solution

  • You can use this regex and capture your digits from group1,

    \bsensitivity\s*"(\d+)"
    

    As you want to select 99 which is only after sensitivity as whole word, word boundaries \b needs to be used surrounding the word, like \bsensitivity\b and \s* allows matching optional whitespace between the word and then " matches a doublequote then (\d+) matches one or more digits and captures in group1 and finally " matches the closing doublequote.

    Regex Demo