Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: regex search for words with one digit

by tybalt89 (Prior)
on Sep 21, 2020 at 17:22 UTC ( #11122013=note: print w/replies, xml ) Need Help??


in reply to regex search for words with one digit

The exclusion trick: \w and [^\W] match exactly the same thing, so to match a \w but not a \d, just use [^\W\d]

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11122003 use warnings; my $text = "John P5ete Andrew Richard58 Nic4k Le7on5"; my @names = $text =~ /\b[^\W\d]*\d[^\W\d]*\b/g; print "@names\n";

Outputs:

P5ete Nic4k

Replies are listed 'Best First'.
Re^2: regex search for words with one digit
by AnomalousMonk (Bishop) on Sep 21, 2020 at 18:25 UTC
    /\b[^\W\d]*\d[^\W\d]*\b/

    Note that this matches the name '1'.


    Give a man a fish:  <%-{-{-{-<

      Rereading the spec, the name '1' is valid. :)

      If the spec is changed, I'll change my regex, but it will cost extra...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2020-10-24 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (242 votes). Check out past polls.

    Notices?