Regex Match string after keyword between brackets

203 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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

0
On

You can use simply this:

(\w+)\s?"(\d+)"

Which outputs

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

For this:

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"

You can put it into an object and then query that object later:

var parse = function(content) {
  var myregexp = /(\w+)\s*"(\d+)"/mg;
  var match = myregexp.exec(content);
  while (match != null) {
    // matched text: match[0]
    // match start: match.index
    // capturing group n: match[n]
    console.log(match[1] + " => " + match[2]);
    // re-run the regex for the next item
    match = myregexp.exec(content);
  }
}

parse(document.getElementById("example1").innerHTML);
console.log("-----------");
parse(document.getElementById("example2").innerHTML);
console.log("-----------");
parse(document.getElementById("example3").innerHTML);
<div id="example1">
zoom_sensitivity "2"
sensitivity "99"
m_rawinput "0"
m_righthand "0"
</div>

<div id="example2">
sensitivity"99"m_rawinput"0"zoom_sensitivity"2"m_righthand"0"
</div>

<div id="example3">
sensitivity"99" m_rawinput "0"
m_righthand "0"
zoom_sensitivity"2"
</div>