Regular expression don't match sequence of characters

B

Big Tony

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
 
T

Tad McClellan

Big Tony said:
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";
}
 
A

Anno Siegel

Big Tony said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top