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.
[] 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 .
$ 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.
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”.
(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With