Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regex match on nothing

Reply
Thread Tools

regex match on nothing

 
 
John
Guest
Posts: n/a
 
      03-04-2010
Hi

my $x='Fred's book';
or $x="Fred's book";
or $x-'Freds book';

if ($x =~ s /Fred('|\')s book/xxxxx/);

How can I modify this when there is nothing between Fred and s?

Regards
John





 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      03-04-2010
John wrote:
> Hi
>
> my $x='Fred's book';
> or $x="Fred's book";
> or $x-'Freds book';
>
> if ($x =~ s /Fred('|\')s book/xxxxx/);
>
> How can I modify this when there is nothing between Fred and s?


Just put a question mark after the closing parenthesis:

if ($x =~ s/Fred('|\')?s book/xxxxx/) {


--
These are my personal views and not those of Fujitsu Technology Solutions!
Josef Möllers (Pinguinpfleger bei FTS)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      03-04-2010
John wrote:
> Hi
>
> my $x='Fred's book';
> or $x="Fred's book";
> or $x-'Freds book';
>
> if ($x =~ s /Fred('|\')s book/xxxxx/);
>
> How can I modify this when there is nothing between Fred and s?


Add a third alternative, zero-width match:

if ( $x =~ s/Fred(?:'|'|)s book/xxxxx/ )



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
 
Reply With Quote
 
John
Guest
Posts: n/a
 
      03-05-2010

"John" <(E-Mail Removed)> wrote in message
news:hmo1kd$glb$(E-Mail Removed)...
> Hi
>
> my $x='Fred&#39;s book';
> or $x="Fred's book";
> or $x-'Freds book';
>
> if ($x =~ s /Fred(&#39;|\')s book/xxxxx/);
>
> How can I modify this when there is nothing between Fred and s?
>
> Regards
> John
>
>
>



Many thanks. John.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
re module: Nothing to repeat, but no sre_constants.error: nothing torepeat ? Devin Jeanpierre Python 2 02-14-2012 01:33 PM
Regex question for String.replace; match whitespace OR nothing jwcarlton Javascript 1 02-05-2011 09:39 AM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Match url beginning with http:// or nothing. Heckner Perl Misc 3 07-11-2005 09:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments