Search code examples
stringperlsearchperl-module

search a specific sub string pattern in a string using perl


I'm a newbie to perl, I went through this Check whether a string contains a substring to how to check a substring is present in a string, Now my scenario is little different

I have a string like

/home/me/Desktop/MyWork/systemfile/directory/systemfile64.elf ,

In the end this might be systemfile32.elf or systemfile16.elf,so In my perl script I need to check whether this string contains a a substring in the format systemfile*.elf. How can I achieve this in perl ?

I'm planing to do like this

if(index($mainstring, _serach_for_pattern_systemfile*.elf_ ) ~= -1) {
    say" Found the string";
}

Solution

  • You can use the pattermatching

    if ($string =~ /systemfile\d\d\.elf$/){
       # DoSomething
    }
    

    \d stands for a digit (0-9)

    $ stands for end of string