Re: How to search/match a variable pattern



Hi John,

Thanks for a very quick response John.

It gives the error as below:

Trailing \ in regex m/D:\STUDY\PERL\MYFTP\/ at D:\Study\Perl\MyFtp\Ftp1.pl line
109.

($base,$path,$type) = fileparse($ARGV[0]);
$pattern=$path;
print "matches" if $string =~ m/$pattern/;

I print $pattern and it is D:\STUDY\PERL\MYFTP\. You are probably right that
there are some meta-characters, \ :, that I need to escape, but how do I escape
them if they are variables? Basically, I actually just want to search for
whether I found a match of a particular string on $path and do not need to
assign it to $string, how do I do that? Am not sure if am doing it right
really, so in theory, I just want to do something like this ...

if ($path =~ m/$pattern/) {
print 'match';
... do something here ...
} else {
print 'no match';
... do something here ...
}

Or do I get the regex logic all wrong ...

The one liner is just a test on whether am using the pattern matching correctly
or not.

Also, how do I do a non-case sensitive match. At the moment, am using uc() to
upper case both the string and the pattern that I am searching for.


Quoting "John W. Krahn" <krahnj@xxxxxxxxx>:

benbart@xxxxxxxxxxxx wrote:
Hi all,

Hello,

Can some please advise how to get this to work?

print "matches" if $string =~ m/$pattern/;

Am trying to search for a pattern but the pattern has to be a variable. Thanks
in advance.

How did this "not work"? What is it not doing that you want it to do? What
warning/error messages did it generate?

You probably have regex meta-characters in $pattern that need to be escaped.


John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
<http://learn.perl.org/> <http://learn.perl.org/first-response>




.



Relevant Pages

  • Re: Regex question
    ... structure of the date you're trying to extract. ... For example, in Regex you can ... pattern that will ensure a valid date within the range allowed by T-SQL ... valid date from a string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regex
    ... case sensitivity was part of the problem which I fixed with ... In other word I got syntax problem with the month pattern ... >>against the articles and help on regex, I still can't find the mistake I ... >> Public Function regtest(ByVal StringIn As String, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How do I shorten or split this function
    ...    (scan regex string) ... Instead of field-name you mean field-type. ... it doesn't test the string to see if it's of the suitable form for the ... corresponding field of the pattern. ...
    (comp.lang.lisp)
  • Re: Expressing AND, OR, and NOT in a Single Pattern
    ... much better because the esoterica of regex can make the desired ... results hard to figure out and the bugs in the pattern even harder to ... The only thing I'd do differently to these patterns is add an anchor ... long strings, when the pattern was at the end of the string. ...
    (comp.lang.perl.misc)
  • Re: Interaction between two strings
    ... then the string XYZC should give a match. ... > string that is supplied separately from the regex. ... $patterns, you splice them all into one very big $pattern. ... Doing this is immensely faster than iterating through an array or the like: ...
    (comp.lang.perl.misc)