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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.