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 that there are
some meta-characters, \ :, that I need to escape, but how do I escape them if
they are variables?

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: Help with Regex and trying to mimic the VB "like" comparison
    ... I'd suggest using Regex.Escape(...) to escape all the meta-characters in the ... just allow the pattern the user types be the ... pattern for the regex, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex doubt
    ... > really dont want to use HtmlAgilitypack - I'm learning RegEx and want to ... >>> exp is braindead as it chokes on nested tags. ... >>> says something like - 'match any text that doesnt match this ... >>>, within a pattern, match the pattern itself?In that case, the first ...
    (microsoft.public.dotnet.framework)
  • Re: string.remove bringt nicht das erwartete
    ... Ein Regex ist da etwas flexibler. ... pattern, "", System.Text.RegularExpressions.RegexOptions.IgnoreCase) ... Wenn man nun aber nach einem Backslash sucht ... gleichen gilt für alle anderen Metazeichen. ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: limits on regex?
    ... Same regex could work and fail on 2 different strings. ... not on the complexity of regex expression. ... storing / changing documents in DB and keep resulting pattern description ... > Does anyone have any hard data on the size / complexity limits of the ...
    (microsoft.public.dotnet.framework)
  • (patch for Bash) match, strinterval
    ... These 2 builtin commands can do regex or fixed-string matching. ... Return success if STRING contains REGEX pattern. ... which case SUBMATCH will contain 3 elements, ...
    (comp.unix.shell)