Help with basic regex

Mr Scratch

Expert Member
Joined
May 15, 2013
Messages
4,838
Hi all,

I have a long line that I would like to extract a specific string from using regex, the line would look like:

Code:
<thing>thing</thing><url href="/orders/[B]history.pl?order=randomstring[/B]"<tag>stuff</tag>

Taking a look at the part I would like:

Code:
history.pl?order=randomstring"

The data will always start with history.pl?order= and ends with a "

Could anyone please assist me with building a regex that would allow me to extract this, the part after the order= is not of set length but since its delimited with "

Thank you
 

Other Pineapple Smurf

Honorary Master
Joined
Jun 21, 2008
Messages
14,593
I'm able to extract using the following

Code:
/history.pl\?order=(.*)"/

This will extract any value for order. If you wanted to limit this to decimals then you should change it to:

Code:
/history.pl\?order=(\d*)"/

Depending on your exact needs, you might need to change the quantifier from * (match 0 or more {0,} ) to + (match 1 or more {1,}).

Another question is which language? I tested this using PHP

PHP:
$test='<thing>thing</thing><url href="/orders/history.pl?order=randomstring"<tag>stuff</tag>';
$regex='/history.pl\?order=(.*)"/';
$match=Null; 
if(preg_match($regex, $test, $match)) {
  echo 'Found: '.$match[1].PHP_EOL;
} else {
  echo '* No match found *'.PHP_EOL;
}


Cheat sheet to help:

https://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
 
Top