Solved

want everything above the : [space(s)] > pattern

Posted on 2013-02-05
10
234 Views
Last Modified: 2013-02-07
want regex pattern to accept everything above

wrote: >


Will be there 1215 recipient wrote: >sender


note: there are 1+ [space] between
:
and
>


for example
Will be there 1215 recipient wrote:                                              >sender




or new lines and spaces
Will be there 1215 recipient wrote:                                              


>sender






want
Will be there 1215 recipient
0
Comment
Question by:rgb192
  • 5
  • 5
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38858173
You haven't indicated which language or editor you are using, so I'll offer one in the hopes that it works with your environment. Try:

[\s\S]+?(?=wrote: +>)

Open in new window

0
 

Author Comment

by:rgb192
ID: 38859358
the question is for php
sorry for not putting php zone

$body=' Will be there 1215 recipient wrote: >sender ';
$body=' Will be there 1215 recipient wrote: 
>sender ';

//$string = 'wrote: >';
$string = '[\s\S]+?(?=wrote: +>)';
$pattern = preg_quote($string, "#"); #escape special characters, and the # character for use as the delimiter
$new_body = preg_replace("#^(.*?){$pattern}.*#s", "$1", $body);

print $new_body;

Open in new window



I can not make the new $pattern work for either $body

$body=' Will be there 1215 recipient wrote: >sender ';
$body=' Will be there 1215 recipient wrote:
>sender ';

the old $pattern only works for
 'wrote: >';
and not
'wrote: [space(s) and new line(s)]>';
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38859513
You're preg_quote'ing the pattern. This effectively makes it raw text. In other words, preg_quote is negating anything "pattern-like" about the pattern I put forth. Why are you using preg_quote?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:rgb192
ID: 38859606
because expert suggested this

http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q_28020043.html

if you have a better way, than it is okay
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38859900
Well it fits for that example, but what I proposed should actually be used as a pattern, and not preg_quote'ed. So change your logic to the following and try executing the match:

$body=' Will be there 1215 recipient wrote: >sender ';
$body=' Will be there 1215 recipient wrote: 
>sender ';

$string = '[\s\S]+?(?=wrote: +>)';
$pattern = $string;
$new_body = preg_replace("#$pattern#", "$0", $body);

print $new_body;

Open in new window

0
 

Author Comment

by:rgb192
ID: 38860116
your output is:
Will be there 1215 recipient wrote: >sender
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38860404
My mistake. Try:

$body=' Will be there 1215 recipient wrote: >sender ';
$body=' Will be there 1215 recipient wrote: 
>sender ';

$string = 'wrote:\s+>[\s\S]';
$pattern = $string;
$new_body = preg_replace("#$pattern#", "", $body);

print $new_body;

Open in new window

0
 

Author Comment

by:rgb192
ID: 38862194
your output is
Will be there 1215 recipient ender
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38863664
It's amazing how much difference a character can make  : )

Sorry, I forgot the +:

$string = 'wrote:\s+>[\s\S]+';

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 38863792
thanks

it is amazing the difference 30 characters (that you showed me) makes
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question