Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

"exact" pattern matching

by my_nihilist (Sexton)
on Jun 08, 2008 at 21:06 UTC ( [id://690956]=perlquestion: print w/replies, xml ) Need Help??

my_nihilist has asked for the wisdom of the Perl Monks concerning the following question:

Does perl have an exact pattern match feature so that i could accept a user-input string without having to parse it for regexp special characters?

Replies are listed 'Best First'.
Re: "exact" pattern matching
by CountZero (Bishop) on Jun 08, 2008 at 21:14 UTC
    index seems to fit the requirement:
    The index function searches for one string within another, but without the wildcard-like behavior of a full regular-expression pattern match. It returns the position of the first occurrence of SUBSTR in STR at or after POSITION. If POSITION is omitted, starts searching from the beginning of the string. POSITION before the beginning of the string or after its end is treated as if it were the beginning or the end, respectively. POSITION and the return value are based at 0 (or whatever you've set the $[ variable to--but don't do that). If the substring is not found, index returns one less than the base, ordinarily -1.
    Thus quoth the docs -- Amen!


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: "exact" pattern matching
by moritz (Cardinal) on Jun 08, 2008 at 21:24 UTC
    Others have rightly mentioned index and eq.

    There's also quotemeta and m/\Q$regex\E/ for escaping strings in a regex. (If you don't do anything else, the regex doesn't have any benefit over index, though. Except perhaps ease of use).

      hmmm...i see a pattern.

Re: "exact" pattern matching
by GrandFather (Saint) on Jun 08, 2008 at 21:16 UTC

    Does eq (see Equality Operators) do what you want?

    Perl is environmentally friendly - it saves trees
      No, I need to use it in a "search and replace" style. I think the only way to do that with "eq" would be to take the length of the string and then compare every segment of that length, advancing thru the data one character at a time. Parsing the string first will be easier.

      Tk::text DOES have an "exact" match option (like the ones in your word processor, web browser, etc.) but i then need a way to apply the string from the GUI in searches of files that, for example, have not yet been loaded into the Tk GUI. I do think it's odd that we can't go:
      if ($str=~/^?.../E)
      where "E" would meaning ignoring special characters -- the equivalent of:
      if ($str=~/\^\?\.\.\./)
        What's wrong with index?? And you can of course ignore special chars in a regex.
        $str=~/\Q \E/
        Upps, did not see the posting of moritz...
Re: "exact" pattern matching
by jshin (Novice) on Jun 09, 2008 at 02:26 UTC
    Not exactly sure what you mean by exact pattern matching...

    Can you give an example of what you are trying to do?

    You could always do something like

    if($foo =~ m/quotemeta($user_input)/) {
    print 'matches!';


    if($foo eq $user_input) {
    print 'matches!';

    Probably not the answer you were looking for, but not sure what you mean by "exact" pattern matching.. Please give examples.
Re: "exact" pattern matching
by wade (Pilgrim) on Jun 09, 2008 at 15:55 UTC

    Not sure how trusted your users are but, once you're done solving this problem, you might want to check out taint mode (do a perldoc perlsec).

Re: "exact" pattern matching
by spacebat (Beadle) on Jun 10, 2008 at 02:13 UTC

    I agree with CountZero, it sounds like you want index() to get the starting position of the substring, then you can use substr() as an lvalue to assign your replacement string.

    However this sounds like what regular expressions were designed for, so either quotemeta() or the following may be a better fit:

Re: "exact" pattern matching
by smokemachine (Hermit) on Jun 09, 2008 at 14:09 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://690956]
Approved by GrandFather
Front-paged by CountZero
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-19 20:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found