Qasim Ahmed
Qasim Ahmed

Reputation: 197

Using named captures with the branch reset pattern

$recepies =~ s/
                    (?|
                    ([^\.\/]) ($dimentions) 
                    |
                    (\(?) ($wholeNumberDecimal)     #ex: 1.5
                    |
                    (\(?) ($wholeNumber)            #ex: 1
                    |
                    (\(?) ($wholeNumberFraction) 
                    )
                    (\s) ($unit)

                    /transformer($1,$2,$3,$4) /eixg;    #the replacement

I was wondering how would it be possible to name my captures in this context.

For example I would like to call the first one "prefix" the second "number" the third "space" and the fourth "unit"

Upvotes: 0

Views: 87

Answers (1)

user557597
user557597

Reputation:

Probably can be done like this

$recepies =~ s/
     (?|
          (?<prefix> [^\.\/] )                    # (1)
          (?<number> $dimentions )                # (2)
       |  
          (?<prefix> \(? )                        # (1), ex: 1.5
          (?<number> $wholeNumberDecimal )        # (2)
       |  
          (?<prefix> \(? )                        # (1), ex: 1
          (?<number> $wholeNumber )               # (2)
       |  
          (?<prefix> \(? )                        # (1)
          (?<number> $wholeNumberFraction )       # (2)
     )
     (?<space> \s )                          # (3)
     (?<unit> $unit )                        # (4)

/transformer($+{prefix},$+{number},$+{space},$+{unit}) /eixg;    #the replacement

Or like this

$recepies =~ s/
     (?|
          (?<prefix> [^\.\/] )                    # (1)
          (?<number> $dimentions )                # (2)
       |  
          (?<prefix> \(? )                        # (1), ex: 1.5
          (?<number> $wholeNumberDecimal )        # (2)
       |  
          (?<prefix> \(? )                        # (1), ex: 1
          (?<number> $wholeNumber )               # (2)
       |  
          (?<prefix> \(? )                        # (1)
          (?<number> $wholeNumberFraction )       # (2)
     )
     \s
     $unit

/transformer($+{prefix},$+{number},$unit) /eixg;    #the replacement

Upvotes: 1

Related Questions