in reply to In my it is printing in the else i want to get output for the for loop in linux.

This is bizarre to me: m/^[\w\W\s\S]+...

This means the line starts with one or more characters which are either a word character or not a word character or a space character or not a space character...

That is nonsense and matches anything.
Describe in words that you are trying to match.

  • Comment on Re: In my it is printing in the else i want to get output for the for loop in linux.
  • Download Code

Replies are listed 'Best First'.
Re^2: In my it is printing in the else i want to get output for the for loop in linux.
by hippo (Archbishop) on Mar 29, 2022 at 10:15 UTC
      Well one would hope that "in words" means showing some examples in order to fully describe what is supposed to happen. I really have no idea from the OP's current code this thing is supposed to do. I will add "show some actual examples of match and no match" in future such requests.

      Test::More is fine, but something even easier could be fine too. The OP could get his point across by flushing this out with more examples...

      use strict; use warnings; #wild ass guess foreach (qw( /HD123FA45 /HD123FA45/anything anything/HD123FA45 /FC/blah /LM /H4p5 asdf/blah/x/H4p9 /asdf7758 )) { print "$_ "; if (/\/(H|HD)[0-9]{1,3}(FA|FC|C|P|CO)[0-9]{1,3}$/i or /\/(FC|LM|CO)/i) { print "\t...match!\n"} else {print "\t...no match!\n";} } __END__ /HD123FA45 ...match! /HD123FA45/anything ...no match! anything/HD123FA45 ...match! /FC/blah ...match! /LM ...match! /H4p5 ...match! asdf/blah/x/H4p9 ...match! /asdf7758 ...no match!
Re^2: In my it is printing in the else i want to get output for the for loop in linux.
by afoken (Chancellor) on Mar 29, 2022 at 06:30 UTC

    This is bizarre to me: m/^[\w\W\s\S]+...

    This means the line starts with one or more characters which are either a word character or not a word character or a space character or not a space character...

    No, that means the line starts with one or more of of \, w, W, s, S. Your description would be right if the code was m/^(\w|\W|\s|\S)+...

    Update: The above is wrong. See below.

    [...] in a regexp is for a character class. (...) is for grouping. See perlre.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      I am not sure about that.
      It is perfectly allowed to use a character class Perl short-cut within brackets, [\d] means the same as [0-9]
      [\w] would mean same as [a-zA-Z0-9_]
      use strict; use warnings; my $x = 'a34x5'; my @y = $x =~ /([\d]+)/g; my @z = $x =~ /([0-9]+)/g; print "@y\n"; # 34 5 \d worked fine print "@z\n"; # 34 5 my @b = $x =~ /([\d\w]+)/g; print "@b\n"; # a34x5 \d irrelevant but \w works my @c = $x =~ /([\w]+)/g; #brackets not needed print "@c\n"; #a34x5 my @d = $x =~ /(\w+)/g; print "@d\n"; #a34x5
      No matter what, the OP's code is bizarre.

      Added: The idea of using an anchor to the beginning of the string, followed by any amount of random stuff, makes no sense to me. Better to leave that off entirely (don't put /^.*(match)/, just put /(match)/.

      I think if you want \ in the character set, you have to escape it with another \

        I am not sure about that.

        It is perfectly allowed to use a character class Perl short-cut within brackets

        I get the same results, but I wonder where that is documented ...

        (Some doc searching and a short meeting later ...)

        It's hidden in perlrecharclass:

        You can put any backslash sequence character class (with the exception of \N and \R) inside a bracketed character class, and it will act just as if you had put all characters matched by the backslash sequence inside the character class. For instance, [a-f\d] matches any decimal digit, or any of the lowercase letters between 'a' and 'f' inclusive.

        [...]

        Examples:

        /[\p{Thai}\d]/ # Matches a character that is either a Thai # character, or a digit. /[^\p{Arabic}()]/ # Matches a character that is neither an Arabic # character, nor a parenthesis.

        So I stand corrected.


        [\d] means the same as [0-9] [\w] would mean same as [a-zA-Z0-9_]

        Well, that's only true if you ignore Unicode. perlre points to perlunicode, which has this nice short explaination:

        \p{Word}
        This is the same as \w, including over 100_000 characters beyond ASCII.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)