regex query

S

Steve Dunn

Hi all,
Just a quick question for someone that might have the answer.
I need a regex that'll turn ? and * that are not in quotes into the regex
equivalant (? to .{1} and * to .*?)
For instance,

"ig*ore st?ff in qu*tes" t?p high*

would be:

"ig*ore st?ff in qu*tes" t.{1}p high.*?

So, to recap, a regex that'll replace * and ? into the regex equivalant.

Cheers!

Steve
 
M

Matt Garrish

s/("[^"]*")|(\*)|(\?)/$2 ? '.*?' : ($3 ? '.{1}' : $1)/eg;

He never said whether the asterisks and question marks could be escaped to
be literals. Your regex should work fine so long as that is not the case.

Matt
 
M

Mike Flannigan

Gunnar said:
s/("[^"]*")|(\*)|(\?)/$2 ? '.*?' : ($3 ? '.{1}' : $1)/eg;

I'm trying to figure this regex out. I can follow a bunch of
it - especially the first "search" part. But I can't find
reference to what those colons ( : ) are in the replacement
part.

I'm a bit surprised by the order of the replacement part also.

Can anybody show me where I can read up on this? I went
through the entire regex chapter in Beginning Perl, but could
not locate it.


Mike
 
M

Matt Garrish

Mike Flannigan said:
I'm trying to figure this regex out. I can follow a bunch of
it - especially the first "search" part. But I can't find
reference to what those colons ( : ) are in the replacement
part.

He's using the ternary operator (?:) to find out which case has been
captured and what to do. The order makes it easier to read (check $2 and $3
first since they're they only cases that need a substitution, and finally
check $1 and insert it back in if that's what was captured). Take a look at
the perlop man page for more info
(http://www.perldoc.com/perl5.8.0/pod/perlop.html#Conditional-Operator).

Matt
 
M

Mike Flannigan

Matt said:
He's using the ternary operator (?:) to find out which case has been
captured and what to do. The order makes it easier to read (check $2 and $3
first since they're they only cases that need a substitution, and finally
check $1 and insert it back in if that's what was captured). Take a look at
the perlop man page for more info
(http://www.perldoc.com/perl5.8.0/pod/perlop.html#Conditional-Operator).

Matt

Thank you. I saw the (?: . . ) - no back reference extended expression,
but this is obviously different.

Obviously you can put this on the search side also. Very interesting.


Mike
 
G

Gunnar Hjalmarsson

Mike said:
I saw the (?: . . ) - no back reference extended expression,
but this is obviously different.

Well, yes. Actually it has nothing to do with the regexp syntax. Since
I used the /e modifier, the right side is evaluated as an expression.
This does the same:

s/("[^"]*")|(\*)|(\?)/
if ($2) {
'.*?';
} elsif ($3) {
'.{1}';
} else {
$1;
}
/eg;

The replacement text is simply the return value from the expression.

Hope that helps.
 
G

Gunnar Hjalmarsson

Matt said:
s/("[^"]*")|(\*)|(\?)/$2 ? '.*?' : ($3 ? '.{1}' : $1)/eg;

He never said whether the asterisks and question marks could be
escaped to be literals. Your regex should work fine so long as that
is not the case.

True. To be honest, I've no good idea of what OP intends to do with
the substituted text. :)
 
B

Bob Walton

Mike said:
Matt Garrish wrote:
....



From a previous post in this thread:

s/("[^"]*")|(\*)|(\?)/$2 ? '.*?' : ($3 ? '.{1}' : $1)/eg;

Thank you. I saw the (?: . . ) - no back reference extended expression,
but this is obviously different.

Obviously you can put this on the search side also. Very interesting.


Note the /e switch on the end of the s/// operator (in addition to the
/g switch). The /e switch causes the replacement part of the s///
operator to be evaluated as a Perl program, and the value from the last
statement executed in that progrm to be used for the substitution. So
in this context, the ? is the usual ternary conditional operator, and
has nothing to do with a regexp.
 
B

Big and Blue

Steve said:
Hi all,
Just a quick question for someone that might have the answer.
I need a regex that'll turn ? and * that are not in quotes into the regex
equivalant (? to .{1} and * to .*?)

FWIW.

.{1} is simpler to read (and write) as just . - it is what . means!
 
M

Matt Garrish

Big and Blue said:
FWIW.

.{1} is simpler to read (and write) as just . - it is what . means!

And ? is not the equivalent of .{1} or ., in his case, but of .{0,1}. I
assume the .{1} was just a typo.

Matt
 
B

Benjamin Goldberg

Steve said:
Hi all,
Just a quick question for someone that might have the answer.
I need a regex that'll turn ? and * that are not in quotes into the
regex equivalant (? to .{1} and * to .*?)
For instance,

"ig*ore st?ff in qu*tes" t?p high*

would be:

"ig*ore st?ff in qu*tes" t.{1}p high.*?

So, to recap, a regex that'll replace * and ? into the regex equivalant.

Cheers!

my %trans = (qw/ * .*? ? .?? { ( , | } ) /, "", "");
(my $regex = $glob_pattern) =~ s<
( [^*?{,}]* )( [*?{,}]? )
quotemeta($1) . $trans{$2};
}xge;
$regex = '^' . $regex . '$';

[untested]
 

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

Forum statistics

Threads
474,293
Messages
2,571,505
Members
48,192
Latest member
LinwoodFol

Latest Threads

Top