1

I am trying to access this only parts of user input. Eg. if user inputs a file name like /file/path/test_324_3422.jpg

I want to get only test, 324 and 3422

I have been attempting this, but it does not work:

#!/usr/bin/perl

if(@ARGV != 1){
    print "Error\n";
}
else{
    my $input = $ARGV[0];
    $input =~ /(.)_([\d]+)_([\d]+)/
    print $1, "\n";


}
2
  • 1
    regex101.com/r/sA4fK9 Commented May 20, 2014 at 4:19
  • $1 refers to your first capturing group, if you want to display the content of group 2 and 3, use $2 and $3. Change your first group description to ([^\/_]+) Commented May 20, 2014 at 4:25

1 Answer 1

1

The dot . inside your capture group is only matching a single character.

And you are only printing $1 which is the match of the first capturing group. You need $2 and $3 also if you want to display the matches of those capturing groups as well.

my $input =~ /([a-zA-Z]+)_(\d+)_(\d+)/;
print join(', ', $1, $2, $3), "\n";

If that does not suit your needs, use a negated match for your first capturing group.

my $input =~ /([^\/_]+)_(\d+)_(\d+)/;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.