DSLs in perl and python (Was sobering observation)
Jussi Piitulainen
jussi.piitulainen at helsinki.fi
Fri Mar 18 09:18:48 EDT 2016
Rustom Mody writes:
> On Friday, March 18, 2016 at 4:17:06 AM UTC+5:30, MRAB wrote:
>> Stick an "x" on the end of the regex: /something/x or s/old/new/x.
>
> Thanks!
>
> Is there somewhere a regexp 'introspection' API/capability available?
>
> ie if the re looks like
> rexp = r"""
> # DSL (instantiation) for describing NYSE symbology
> ^
> (?P<scrip> [A-Z]*) # The base scrip
> (?P<serchar> [.+-])? # Series type char
> (?P<series> [A-Z])? # Series
> (?P<issuedc> [#])? # issued char indicator
> $ # Thats all (there should be!)
> """
>
> I would like to know that the named-groups are
> {scrip, serchar, series, issued}
> without doing match/search etc
>
> That way then the same DSL could be used for quite different regexps
>
> IOW I would like to generalize the code:
>
> g = m.group
> scrip, serchar, series, issuedc = g('scrip'), g('serchar'), g('series'), g('issuedc')
>
> The scrip, serchar, series etc need not be local vars;
> a dict is fine/preferable
Maybe the .groupindex in the object that you get when you compile the
regex. Found it with dir(pat), help(pat) was not so helpful. Haven't
looked for other documentation.
>>> pat = re.compile(r'...(?P<foo>xxx)...(?P<bar>yyy)...')
>>> pat.groups
2
>>> pat.groupindex
{'bar': 2, 'foo': 1}
>>> pat.pattern
'...(?P<foo>xxx)...(?P<bar>yyy)...'
More information about the Python-list
mailing list