Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regular expression don't match sequence of characters

Reply
Thread Tools

Regular expression don't match sequence of characters

 
 
Big Tony
Guest
Posts: n/a
 
      09-27-2004
Hi

I suspect this is an easy one and sits somewhere in a FAQ, but can
anyone help anyway?

Suppose I have two strings:
$str1 = "abcd%efg";
$str2 = "1234\%567";

I would a regular expression that will return "abcd" from $str1 and
"1234\%567" from $str2, i.e. if a "%" is encountered without being
preceded by a "\" then ignore it and the remainder of the string, but
if "\%" is encountered do nothing special and return it with the rest
of the string.
I guess I could use split to break the strings around the "%" and then
test for a trailing "\", but wondered if anyone could come up with a
one-liner.

cheers

BT
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-27-2004
Big Tony <> wrote:

> Suppose I have two strings:
> $str1 = "abcd%efg";
> $str2 = "1234\%567";



I guess you haven't tried print()ing the value of $str2 at this point, huh?


> I would a regular expression that will return "abcd" from $str1 and
> "1234\%567" from $str2, i.e. if a "%" is encountered without being
> preceded by a "\" then ignore it and the remainder of the string, but
> if "\%" is encountered do nothing special and return it with the rest
> of the string.



None of your strings have a percent preceded by a backslash...


Anyway, you can use a "negative look-behind":

-------------------------------------
#!/usr/bin/perl
use warnings;
use strict;

foreach ( 'abcd%efg', '1234\%567' ) {
print "string is '$_'\n";
# my( $stuff ) = /^(.*?)((?<!\\)%|$)/;
my( $stuff ) = /^(.*?) # bunch of chars until...
(
(?<!\\)% # ... a percent without preceding backslash
| # or
$ # end of string
)/sx;
print "stuff is '$stuff'\n";
}
-------------------------------------


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      09-27-2004
Big Tony <> wrote in comp.lang.perl.misc:
> Hi
>
> I suspect this is an easy one and sits somewhere in a FAQ, but can
> anyone help anyway?
>
> Suppose I have two strings:
> $str1 = "abcd%efg";
> $str2 = "1234\%567";


Note that $str2 does *not* contain a backslash. Either use single
quotes, or double the \:

$str2 = '1234\%567';
or
$str2 = "1234\\%567"

> I would a regular expression that will return "abcd" from $str1 and
> "1234\%567" from $str2, i.e. if a "%" is encountered without being
> preceded by a "\" then ignore it and the remainder of the string, but
> if "\%" is encountered do nothing special and return it with the rest
> of the string.
> I guess I could use split to break the strings around the "%" and then
> test for a trailing "\", but wondered if anyone could come up with a
> one-liner.


Let's first find a regex that matches a single "%", but not a "\%".
You want negative lookbehind for that:

/(?<!\\)%/

(Again, \ must be doubled in double-quotish context.)

To delete that match, plus everything that follows, do

s/(?<!\\)%.*//;

Anno
 
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
Negate a character sequence in a regular expression? crm_114@mac.com Ruby 11 12-02-2007 09:15 AM
Match First Sequence in Regular Expression? Roger L. Cauvin Python 43 01-28-2006 03:39 PM
how to match regular expression from right to left Liang Perl 2 08-27-2004 10:03 PM
match three digit number using regular expression championsleeper Perl 6 04-06-2004 08:54 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57