Solved

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

Posted on 2013-02-05
10
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

730 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