Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - how to extract text from between quotes and exclude quotes

Tags:

regex

I need help with regex. I need to create a rule that would preserve everything between quotes and exclude quotes. For example: I want this...

STRING_ID#0="Stringtext";

...turned into just...

Stringtext

Thanks!

like image 406
user1028096 Avatar asked Nov 03 '11 16:11

user1028096


1 Answers

The way to do this is with capturing groups. However, different languages handle capturing groups a little differently. Here's an example in Javascript:

var str = 'STRING_ID#0="Stringtext"';
var myRegexp = /"([^"]*)"/g;
var arr = [];

//Iterate through results of regex search
do {
    var match = myRegexp.exec(str);
    if (match != null)
    {
        //Each call to exec returns the next match as an array where index 1 
        //is the captured group if it exists and index 0 is the text matched
        arr.push(match[1] ? match[1] : match[0]);
    }
} while (match != null);

document.write(arr.toString());

The output is

Stringtext
like image 140
dallin Avatar answered Nov 14 '22 04:11

dallin