Solved

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

Posted on 2013-02-05
10
226 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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,…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now