Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: In my it is printing in the else i want to get output for the for loop in linux.

by Marshall (Canon)
on Mar 29, 2022 at 09:15 UTC ( [id://11142491]=note: print w/replies, xml ) Need Help??


in reply to Re^2: In my it is printing in the else i want to get output for the for loop in linux.
in thread In my it is printing in the else i want to get output for the for loop in linux.

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 \

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

Replies are listed 'Best First'.
Re^4: 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 11:19 UTC

    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". ;-)
      Well, that's only true if you ignore Unicode.

      Indeed. That's why we have the /a and /aa modifiers.


      🦛

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found