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

Re: Defining your own regex character class

by haukex (Abbot)
on Dec 18, 2017 at 08:26 UTC ( #1205776=note: print w/replies, xml ) Need Help??


in reply to Defining your own regex character class

There is an experimental feature (that I haven't used yet), Extended Bracketed Character Classes, that should allow you to define your own classes (or more specifically, compose a character class out of others, which you can also interpolate into a regex).

However, /\b(?:[[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}\b/ is not a character class (emphasis mine):

A character class is a way of denoting a set of characters in such a way that one character of the set is matched. It's important to remember that: matching a character class consumes exactly one character in the source string.

What you've got is a whole regex, and I think that LanX is right that simply compiling that into a variable with qr and interpolating that into another regex is the best way to go. Later in that thread you asked if you could isolate the variable, and the usual ${} can be applied here too, as well as the /x modifier:

my $x = qr/foo/; my $y = qr/${x}bar/; # (?^:(?^:foo)bar) my $z = qr/ $x bar /x; # (?^x: (?^:foo) bar )

Regarding the example you've shown, note there is $RE{net}{IPv4} from Regexp::Common::net.

Minor edits for clarification.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1205776]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2018-01-21 11:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (227 votes). Check out past polls.

    Notices?