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

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:


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 = ""; if ($string =~ m/$phrase/i){
The above recognizes "" 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

        Note that this also matches 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)
        use feature qw( fc ); if (fc($string) eq fc($phrase))

        The second is probably faster.

        Seeking work! You can reach me at

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 ''; 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( '', $phrase, 'match period' );


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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11135228]
Front-paged by Corion
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
Find Nodes?
    Voting Booth?

    No recent polls found