Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: words with numbers in them

by Paladin (Vicar)
on Jul 31, 2018 at 18:52 UTC ( #1219591=note: print w/replies, xml ) Need Help??


in reply to words with numbers in them

If your definition of "word" in this case, is non-whitespace characters separated by whitespace, then I'd split on white space, then grep the resulting array for elements containing numbers.
my $names = "1Petrus2 Joh4an Smit 2johnny99 Julius 789"; my @words = split /\s+/, $names; my @numwords = grep /\d/, @words;

Replies are listed 'Best First'.
Re^2: words with numbers in them
by facebook (Initiate) on Jul 31, 2018 at 19:19 UTC

    You are wright, silly me. Thank you.

      No need to split and grep, just simplify your regex :)

      #!/usr/bin/perl use strict; use warnings; my $names = '1Petrus2 Joh4an Smit 2johnny99 Julius 789 one 2two three3 fo44ur ..5.. six seven, seven7, eight--8--nine '; while( $names =~ /(\S*\d\S*)/g ) { print "$1\n"; }

        I am trying to understand your regex, but i cannot understand why it is :

        $names =~ /(\S*\d\S*)/g )

        in stead of:

        $names =~ /(\S*\d+\S*)/g

        So i changed it and it seems this also works, so which is it?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2019-10-23 21:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?