Jump to content

Regex query - stripping away unwated text


iLuke

Recommended Posts

If I had the string: Your Chief of Intelligence dispatches 1 spies to attempt to sabotage 16 of Remco-MOD's weapons of type Lookout Tower. Your spies successfully enter Remco-MOD's armory undetected

 

and I wanted the name of the weapon and knew it could be a variable number of words for the weapon I would use:

<?php
$string = "Your Chief of Intelligence dispatches 1 spies to attempt to sabotage 16 of Remco-MOD's weapons of type Lookout Tower. Your spies successfully enter Remco-MOD's armory undetected";
preg_match('/.*weapons of type ([\w|\s]*)\. Your spies.*/', $string, $matches);
print_r($matches);
?>

 

Tested this code and it was working. Even worked with "Board with a Nail in it" as the weapon.

PS: Explaining the regexp: it is looking for any number (*) of words (\w) or (|) whitespace characters (\s) between the strings on either side of the words and whitespaces. Also note that I had to escape the fullstop (\.) immediately after the weapon, since . is a reserved character in regexps.

Well then thank you very very much for your help!

 

I've learned a lot doing this stuff... it's a pain in the neck if I'm honest, but it sure is useful!

 

The tutorials on the net just aren't really up to standard... it's more of a copy/paste operation from what I've seen.

 

Anyways, thanks again!

Luke.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.