Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Regex question

by kcott (Archbishop)
on Sep 17, 2023 at 09:51 UTC ( [id://11154498]=note: print w/replies, xml ) Need Help??


in reply to Re: Regex question
in thread Regex question

G'day Bill,

I'd say you're on the right track creating a regex for $number and building up a more complex regex from there. Unfortunately, you're matching some things that you shouldn't.

$ perl -E ' say "$_: ", /1?\d{1,4}/ ? "Y" : "N" for qw{240 2 3600 1 10000 0 0000 19999 999999999}; ' 240: Y 2: Y 3600: Y 1: Y 10000: Y 0: Y 0000: Y 19999: Y 999999999: Y

I'd aim for a more stringent regex for $number.

$ perl -E ' say "$_: ", /(?<![0-9])(?:10000|[1-9][0-9]{0,3})(?![0-9])/ ? "Y" : + "N" for qw{240 2 3600 1 10000 0 0000 19999 999999999}; ' 240: Y 2: Y 3600: Y 1: Y 10000: Y 0: N 0000: N 19999: N 999999999: N

The OP is somewhat unclear in that it shows an example with spaces then says spaces are removed. Stuff is also removed, whatever that refers to. There's not much we can do about that beyond requesting clarification.

I'd also add ^ and $ (or equivalent) assertions to the final regex.

— Ken

Replies are listed 'Best First'.
Re^3: Regex question
by Marshall (Canon) on Sep 17, 2023 at 13:19 UTC
    Well, I would use \b,
    say "$_: ", /\b(10000|[1-9][0-9]{0,3})\b/ ? "Y" : "N" for qw{240 2 3600 1 10000 0 0000 19999 999999999};
    Same as your result:
    240: Y 2: Y 3600: Y 1: Y 10000: Y 0: N 0000: N 19999: N 999999999: N
    updated: made capturing group

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-24 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found