Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…

707 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