1

I have a string like this

France Gros Frère et Sur Hte-Cote de Nuit Blc 2008 #wwww #bbbbb 8.5 Nice yellow fruit nose, some vanilla notes, good crispness http: // (www). example. com/23232

I want to extract the string after the 8.5 (In bold) we can use #wwww and #bbbbb they will remain as it is, without even change in number of characters.

This 8.5 can change it can be anything even a 7 or 3.2 etc.

Also how can i exclude a url from the end of the string ?

What is the best way to achieve this with minimum risk of error ?

1
  • @Anirudh i have an idea of exploding with #bbbbb and do something. I expect erros there, so i am looking for a better solution. Commented Jul 8, 2013 at 8:27

3 Answers 3

4

Quick & Dirty:

\#w+ \#b+ \d+(?:\.?\d+)? (.*)

Example:

<?php  
$string = "France Gros Frère et Sur Hte-Cote de Nuit Blc 2008 #wwww #bbbbb 8.5 Nice yellow fruit nose, some vanilla notes, good crispness";  
$regex = "/\#w+ \#b+ \d+(?:\.?\d+)? (.*)/";  
preg_match ($regex, $string, $output);

echo $output[1];
?>

But if there can be a string WITHOUT any number after #bbbbb, you better use this:

\#w+ \#b+\s*(?:\d+(?:\.\d+)?)?\s*(.*)

So you don't have to put any number after #bbbbb and you can use as many spaces as you like between #bbbbb, the number (if there's any) and the string you want to extract.

Most of it is optional, so your string could look like this:

blabla #w #bb Hello World

Or like this

blabla #wwwwwwwwwwwwwwww #bbb 1337 Hello World

Or like this:

#w #bHello World


You can see the result here

EDIT:

As requested, this one should also remove URLs inside of the string:

<?php  
$string = "France Gros Frère et Sur Hte-Cote de Nuit Blc 2008 #wwww #bbbbb 8.5 Nice yellow fruit nose, some vanilla notes, good crispness http://www.example.com/23232";  
$regex = "/\#w+ \#b+ \d+(?:\.?\d+)? (.*)/";  
preg_match ($regex, $string, $output);

if (isset($output[1])) {
    $regex = "!https?:\/\/(?:[\da-z\.-]+)\.(?:[a-z\.]{2,6})(?:[\/\w \.-]*)*\/?!";  
    $newString = trim(preg_replace ($regex, '', $output[1]));

    echo $newString;
} else {
    echo $string;
}
?>

The result should be:

Nice yellow fruit nose, some vanilla notes, good crispness

Sign up to request clarification or add additional context in comments.

3 Comments

Also how can i exclude a url from the end of the string ?
Do you want to exclude every URL or just specific URLs?
Every URL from the end of string
2

Use simple regex

$a='France Gros Frère et Sur Hte-Cote de Nuit Blc 2008 #wwww #bbbbb 8.5 Nice yellow fruit nose, some vanilla notes, good crispness';

preg_match('/\#bbbbb [0-9]+\.[0-9]+ (.*)/', $a, $match);

print_r($match);

[0-9]+ - is a number at least one or more

(.*) - is a subpattern for any character that goes after number.

echo $match[1]; prints what you want :)

Comments

1

Description

This regex will:

  • capture the entire string upto the url at the end, so the url can be excluded if it exists
  • capture the number after #wwww #bbbbb
  • allow the number to contain one or less decimal points

(.*?\#wwww\s\#bbbbb\s((?:\d+\.)?\d+).*?)(https?:\/\/\S*)?$

enter image description here

PHP Example

Sample Text

France Gros Frère et Sur Hte-Cote de Nuit Blc 2008 #wwww #bbbbb 8.5 Nice yellow fruit nose, some vanilla notes, good crispness http://www.example.com/23232

Code

<?php
$sourcestring="your source string";
preg_match('/(.*?\#wwww\s\#bbbbb\s((?:\d+\.)?\d+).*?)(https?:\/\/\S*)?$/imx',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>

Capture Groups

0 has the entire string
1 has the entire string excluding the url at the end if it exists
2 has the desired number
3 has the url

[0] => France Gros Frère et Sur Hte-Cote de Nuit Blc 2008 #wwww #bbbbb 8.5 Nice yellow fruit nose, some vanilla notes, good crispness http://www.example.com/23232
[1] => France Gros Frère et Sur Hte-Cote de Nuit Blc 2008 #wwww #bbbbb 8.5 Nice yellow fruit nose, some vanilla notes, good crispness 
[2] => 8.5
[3] => http://www.example.com/23232

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.