in reply to Capitalization and Regex

$_[0]=~s/(\.|\-|\(|\)|\[|\]|\{|\}|\?|\/|\\|\^|\*)/\\$1/g;
Yikes! So many backslashes, so many |, when you don't need them:
$_ [0] =~ s!([][.(){}?/\\^*-])!\\$1!g;

Abigail

Replies are listed 'Best First'.
Re: Re: Capitalization and Regex
by Anonymous Monk on Oct 27, 2003 at 00:26 UTC
    ::Prints the thread:: S many things to look up! O_O This has been most profitable.
Re: Re: Capitalization and Regex
by pekkhum (Sexton) on Oct 27, 2003 at 01:17 UTC
    Okay, based on what was here I tried to find a better way to assemble my data, here is an example of the way it would look:
    #!perl -w @res='index.cgi'; @ext=qw(css tmp class edf); @regex=''; foreach(@ext){ $reg=$reg."|^.*\\.$_\$"; } foreach(@res){ $reg=$reg."|^\Q$_\E\$"; } if($regex){ $reg="|$regex$reg"; } $thing=~/^\.+$$reg/i;
    I've cut out the source of the actual data, and inserted sample info for simplicity. How is my improvement?
      Um, it's still not clear what you're actually trying to accomplish. There are a handful of odd properties in this snippet of code, including some things that look like mistakes or misconceptions. As many others here would tell you, things would actually go better for you with "use strict;"

      First, I would assume that your declarations at the top were intended to look like this:

      @res = qw/index.cgi/; # could be more than one file... @ext = qw/css tmp class edf/; $regex = ''; # note: "$", not "@"
      Next, since you don't initialize "$reg" to anything prior to the first "for" loop, it comes out starting with "|", which is "grammatical", but probably won't do what you want -- try this:
      perl -e '$_ = "hi"; print "ok\n" if ( /|x/ );
      It will always print "ok", no matter what string is assigned to $_, because a regex that starts with "|" will always match anything (even an empty string). (update: I realize that $reg gets appended to some other literal when it's finally used for matching something; having "|" at the start will still not do what you probably expect/want it to do.)

      BTW, the more legible idiom for concatenating values onto a string inside a loop is:

      $string = "initial value"; for ( @addons ) { $string .= " $_"; # or whatever }
      Next, it's not clear why you have the "if ($regex)" condition, since this variable is apparently empty (false) when you reach this point.

      Finally, the last line is a complete mystery. What does "$thing" contain, and what are you really hoping to match? It looks like you are now trying to use $reg as a reference to a scalar (because of the two dollar-signs); again, this is "grammatical", but $reg is not a reference to a scalar, so you end up with an empty string at that point in the regex.

      Take a few steps back from the minute details, and try to approach it from "the big picture". What is the situation that you are starting with, and what do you want it to be when your code does its job properly?

      If you're just trying to build a regex that will match a given set of file name extensions (e.g. qw/css tmp class edf/), all you need to do is:

      my $ext_regex = '\.(' . join('|', qw/css tmp class edf/) . ')$'; for ( @filenames ) { print "this one matches: $_\n" if ( /$ext_regex/i ); }
        I realize that this script posted doesn't have a purpose and that $thing is undefined. I was looking for the suggestions on structure(which I thank you for providing, I was having trouble with the | and shifted it into a diferent piece of the code). To clarify my meaning: The full program does use strict subs and includes comments on the function of the variables. The variables are designed to allow someone to easily change restrictions on a directory read in a CGI script that functions as a kind of dynamic index file for the page. This is only a boiled down portion I'm using to get advice on better ways to write various commands. In hindsight, I should have left out the whole $regex section, as it is not truely used until much later in the code. ^^;

        Thanks again for the information, you fixed my glitch. ^_^