11

I need to find in a large body of text all the strings that are between = and & symbols. I don't want the result strings to contain = and &, only whats between them.

zzzzBov
  • 157,699
  • 47
  • 307
  • 349
anon
  • 961
  • 3
  • 16
  • 32
  • 2
    if you're parsing query strings, there're better ways of doing it. What language is the regex supposed to be implemented in anyway? – zzzzBov Nov 15 '11 at 18:40
  • I am doing it in jquery, if that doesn't work I will use it in shell with grep – anon Nov 15 '11 at 18:42
  • 2
    You are doing in `JavaScript` then. jQuery is a library whereas JavaScript is a language. It's like asking what language you're speaking and you reply "Library of Congress". – zzzzBov Nov 15 '11 at 18:44
  • If you're parsing the query string you could see http://stackoverflow.com/questions/2090551/parse-query-string-in-javascript or [my answer to a related question](http://stackoverflow.com/questions/7853154/how-to-choose-a-substring-after-given-character/7853496#7853496) – zzzzBov Nov 15 '11 at 18:48

3 Answers3

14

If your regex engine supports lookbehinds/lookaheads:

(?<==).*?(?=&)

Otherwise use this:

=(.*?)&

and catch capture group 1.

If your regex engine does not support non-greedy matching replace the .*? with [^&]*.


But as zzzzBov mentioned in a comment, if you're parsing GET URL prefixes there are usually better native methods for parsing GET arguments.

In PHP for example there would be:

<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
?>

(As found on php.net.)

Edit: Appears you're using Javascript.

Javascript solution for parsing query string into object:

var queryString = {};
anchor.href.replace(
    new RegExp("([^?=&]+)(=([^&]*))?", "g"),
    function($0, $1, $2, $3) { queryString[$1] = $3; }
);

Source: http://stevenbenner.com/2010/03/javascript-regex-trick-parse-a-query-string-into-an-object/

Regexident
  • 29,108
  • 10
  • 91
  • 98
1

Assuming your regex engine supports lookaheads.

/(?<==).*?(?=&)/

Edit :

Javascript doesn't support lookbehind so :

var myregexp = /=(.*?)(?=&)/g;
var match = myregexp.exec(subject);
while (match != null) {
    for (var i = 0; i < match.length; i++) {
        // matched text: match[i]
    }
    match = myregexp.exec(subject);
}

this is what you should use.

Explanation :

"
=       # Match the character “=” literally
(       # Match the regular expression below and capture its match into backreference number 1
   .       # Match any single character that is not a line break character
      *?      # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
)
(?=     # Assert that the regex below can be matched, starting at this position (positive lookahead)
   &       # Match the character “&” literally
)
"
FailedDev
  • 25,171
  • 9
  • 48
  • 70
0
/=([^&]*)&/

You'll of course need to adapt the syntax and what to do with it.

Kevin
  • 48,059
  • 14
  • 92
  • 127