Re: "exact" pattern matching
by CountZero (Bishop) on Jun 08, 2008 at 21:14 UTC
|
| [reply] [d/l] [select] |
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). | [reply] [d/l] |
|
hmmm...i see a pattern.
thanks.
| [reply] |
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
| [reply] |
|
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=~/\^\?\.\.\./)
| [reply] [d/l] [select] |
|
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... | [reply] [d/l] |
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!';
}
or
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. | [reply] |
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).
| [reply] |
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:
s/\Q$pattern\E/$replacement/g
| [reply] [d/l] |
Re: "exact" pattern matching
by smokemachine (Hermit) on Jun 09, 2008 at 14:09 UTC
|
| [reply] [d/l] |