in reply to Generalizing a REGEX

Your code doesn't compile, and it doesn't explain where $section comes from, which seems relevant since you intend for it to be the replacement portion of a substitution operator.

Here's an example of a substitution that does what you're asking for based on the example data you provided:

my @test_cases = ( ['cat.1', 'cat(1)'], ['', ''], ); foreach my $test (@test_cases) { my ($string, $want) = @$test; my $transformed = $string =~ s/\.(\d+)$/($1)/r; print "[$string] --> [$transformed] wanted: [$want]. Success: ", ( +$transformed eq $want ? "yes\n" : "no\n"); }

It's also an example of how to produce an SSCCE. Here's the output it produces:

[cat.1] --> [cat(1)] wanted: [cat(1)]. Success: yes [] --> [] wanted: []. Success: y +es


Replies are listed 'Best First'.
Re^2: Generalizing a REGEX
by Anonymous Monk on Aug 09, 2020 at 16:36 UTC

    Hey Dave Once that regex was fixed, it caused 2 other regex statements to fail. I noticed the '\.' in your statement and was able to fix the other 2 regexes on my own by adding '|\.' to them. ' thought you would like to hear that Thanks again.

Re^2: Generalizing a REGEX
by Anonymous Monk on Aug 08, 2020 at 22:41 UTC

    $section is the section number that comes from the manpage. I wanna replace this foreach() loop with something that is more general. I will hack on this and see if I can make it fit. Thanks