Solved

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

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

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:rgb192
Comment Utility
your output is:
Will be there 1215 recipient wrote: >sender
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
your output is
Will be there 1215 recipient ender
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
thanks

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Python negative lookahead for aaa:bbb@hostname 6 78
Regex Help 2 29
Parse CSS value with RegEx 2 39
Greater than or equal to 2005? 3 26
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…
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…

762 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

7 Experts available now in Live!

Get 1:1 Help Now