Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Perl treats period as space in string

by htmanning (Pilgrim)
on Jul 20, 2021 at 20:03 UTC ( #11135228=perlquestion: print w/replies, xml ) Need Help??

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

Monks,

I have a database of phrases that I'm checking a string against. Perl recognizes all periods as spaces even if I don't want it to. What is the workaround for this?

$phrase = "club.market"; if ($string =~ m/$phrase/i){
The above recognizes "club.market" and "club market" but I only want to recognize the one with the period. What am I missing?

Replies are listed 'Best First'.
Re: Perl treats period as space in string
by tybalt89 (Prior) on Jul 20, 2021 at 20:14 UTC
      Thanks!

        Note that this also matches fooclub.market. Not sure if that's what you want. If you want to perform an exact case-insensitive comparison, use

        if ($string =~ /^\Q$phrase\E\z/i)
        or
        use feature qw( fc ); if (fc($string) eq fc($phrase))

        The second is probably faster.

        Seeking work! You can reach me at ikegami@adaelis.com

Re: Perl treats period as space in string
by choroba (Archbishop) on Jul 20, 2021 at 20:48 UTC
    It's even worse, a dot in a regex matches any character except for a newline.

    Maybe you just want to do

    use feature qw{ fc }; my $phrase = fc 'club.market'; if (-1 != index fc $string, $phrase) { ...

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Perl treats period as space in string
by GrandFather (Saint) on Jul 20, 2021 at 20:59 UTC

    The '.' is matching any non-newline character which is what it does in a regular expression when it hasn't been quoted. See Perl regular expression metacharacters.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: Perl treats period as space in string
by AnomalousMonk (Bishop) on Jul 21, 2021 at 00:57 UTC

      Today I noticed grex ...

      grex is a library as well as a command-line utility that is meant to simplify the often complicated and tedious task of creating regular expressions. It does so by automatically generating a single regular expression from user-provided test cases ...

      This project has started as a Rust port of the JavaScript tool regexgen written by Devon Govett ...

        Similar frak in clojure/cljs which might could be trawled for inspiration as well.

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

Re: Perl treats period as space in string
by perlfan (Vicar) on Jul 20, 2021 at 22:07 UTC
    It's confusing to define the regexp in a variable, then place it there between the /../; The reason is that in the assignment it's very much just a string. When you place it in the regexp context, it suddenly means something different (i.e., the "dot"). So don't do that unless it's very cleary you're dynamically defining your regular expression, otherwise you're just making it more confusing for you and anyone else who may be looking at this even in the very near future.
      You can solve this problem with qr in Regexp Quote Like Operators.
      use strict; use warnings; use Test::More tests => 2; my $phrase = qr/club\.market/; unlike( 'club market', $phrase, 'not match space' ); like( 'club.market', $phrase, 'match period' );

      RESULT,

      1..2 ok 1 - not match space ok 2 - match period
      Bill

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2021-07-24 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?