Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Exact string match

by Anonymous Monk
on Jan 14, 2003 at 07:35 UTC ( [id://226747]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am confused because of Perls efficiency to search for strings that match. Now I would like to restrict this search for exact string: I am looking for string as last word of row
($m='"' and $names[$k] here search string)
: "out.h". Perl is finding also strings like "macout.h", "rowout.h" and so on. I would appreciate If anyone could tell how I should formulate the search string in order to inhibit finding of these not wanted extra string matches? Part of code below
open (INTER, "file_1.txt") || die "File not found"; while (defined ($row = <INTER>)) { if ($row=~ /$m$names[$k]$m$/g)) { if ($row ne $mytext) { $mytext=$mytext.$row; print " $row\n"; } } } close INTER; }
BR Hewarn

Replies are listed 'Best First'.
Re: Exact string match
by theorbtwo (Prior) on Jan 14, 2003 at 08:02 UTC

    I'm not exactly clear on what you're going for here, so I'll just give the short answer. What your're looking for is eq, not a regular expression. In other words, $row =~ /out.h/ will search for things containing the string "out", followed by any single charater, followed by "h". Not only is this not what you want because it matches any substring, but because '.' is a regex metacharater that means "any single character" (other then newline, in some curcimstances). $row eq 'out.h', however, will only succeed (return true) when $row is exactly equal (stringishly speaking) to 'out.h'.

    In not clear on what $m is doing there, but I think what you want is if ($row eq $names[$k]).


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: Exact string match
by BrowserUk (Patriarch) on Jan 14, 2003 at 08:27 UTC

    If I read your post correctly, you want to find strings that match without finding stuff where that string is a substring of a longer string. To do this, you need to know how the line will be delimited in the file. You seem to indicate that it will be quoted with double quotes. In which case, using  if ($row =~ /\"out.h\"$/ ) {...} should be working.

    This appears to be what you are doing with

    if ($row=~ /$m$names[$k]$m$/g))

    if $m = '"' and $names$k = out.h. You don't need the /g option BTW. as there can only be one string at the end of the line ($) and you appear to capturing/printing the whole line anyway. You could also hardcode the "'s into the regex as shown above unless the delimiter can vary?

    One thing that comes to mind is that if this is C or C++ source code and you are looking for lines that include header files or a specific header file, the #include "some.h"; has a semi:colon on the end and you would need to include this into your regex before the $.

    If you are still having trouble, try including a little more of the code (showing where @names and $M are being setup along with a (small) sample datafile on which your code is failing and you'll get better answers.


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Re: Exact string match
by boo_radley (Parson) on Jan 14, 2003 at 16:36 UTC
    well, if you're looking for an exact match, use eq
     if ($row eq "$m$names[$k]$m") {... If you're compelled to use a regex, use anchors to either determine if there's a word in the string that matches your criteria (using the \b anchor)
     if ($row =~ m|\b$m$names[$k]$m\b| {... or the entire string using \A and \z :  if ($row =~ m|\A$m$names[$k]$m\z| {...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://226747]
Approved by vagnerr
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2024-04-16 19:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found