Can I use named groups in a Perl regex to get the results in a hash?

273

Solution 1

Perl uses (?<NAME>pattern) to specify names captures. You have to use the %+ hash to retrieve them.

$variable =~ /(?<count>\d+)/;
print "Count is $+{count}";

This is only supported on Perl 5.10 and higher though.

Solution 2

As of Perl 5.10, Perl regexes support some Python features, making them Python compatible regexes, I guess. The Python versions have the "P" in them, but all of these work in Perl 5.10. See the perlre documentation for the details:

Define a named capture buffer. Equivalent to (?<NAME>pattern).

(?P<NAME>pattern)

Backreference to a named capture buffer. Equivalent to \g{NAME}.

(?P=NAME)

Subroutine call to a named capture buffer. Equivalent to (?&NAME).

(?P>NAME)

Although I didn't add the Python-compatibility to the latest edition of Learning Perl, we do cover the new Perl 5.10 features, including named captures.

Solution 3

AFIK PCRE has named group capturing as:

(?'NAME'pattern)
(?<NAME>pattern)

You can find info here.

Share:
273
schasoli
Author by

schasoli

Updated on June 15, 2022

Comments

  • schasoli
    schasoli almost 2 years

    Using the EmbeddedMediaPlayerComponent of vlcj 3.2.0 the cursor can be disabled with this.setCursorEnabled(false); but this only works on the canvas.

    If VLC is playing via vlcj, the cursor seems to become the default cursor and stays for the default VLC time (1000ms) until the cursor disappears. Using VLC directly, the cursor time can be set to 0ms.

    How can I make the cursor disappear using vlcj. Either setting time to 0ms or using blankCursor?

  • schasoli
    schasoli about 9 years
    --mouse-hide-timeout=0 works well. The mediaPlayer.setCursorEnabled(false) somehow only works an the frame, but not on the canvas. (I put this wrong in my start question) I changed cursor to blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(blankImage, new Point(0, 0), "blank"); and at the constructor canvas.getCursor().getName() returns "blank". So I think it is not a vlcj Problem actually.
  • caprica
    caprica about 9 years
    Did you see the last comment about hiding the cursor before you show your window? If I do it after I show my window, I see a glitched pointer, otherwise it works as expected.