Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex find all the strings preceded by = and ending in &

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.

like image 843
anon Avatar asked Nov 15 '11 18:11

anon


People also ask

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

What matches the start and end of the string?

They are called “anchors”. The caret ^ matches at the beginning of the text, and the dollar $ – at the end. The pattern ^Mary means: “string start and then Mary”.

What does (? I do in regex?

(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.


1 Answers

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/

like image 149
Regexident Avatar answered Oct 27 '22 17:10

Regexident