1

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";
}
Community
  • 1
  • 1

4 Answers4

3

You can use the pattermatching

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

\d stands for a digit (0-9)

$ stands for end of string

simbabque
  • 53,749
  • 8
  • 73
  • 136
Jens
  • 67,715
  • 15
  • 98
  • 113
  • 1
    @HariprasadCR: The index function searches for one string within another, but **without** the wildcard-like behavior of a full regular-expression pattern match – Jens Oct 25 '16 at 09:00
  • @HariprasadCR So for me it sounds like you have to do two separate checks if you want to use `index` – Jens Oct 25 '16 at 09:01
1

Well

if( $mainstring =~ m'/systemfile(16|32)\.elf$' ) {
   say" Found the string";
}

does the job.


For your informations :

$string =~ m' ... '

is the same than

$string =~ / ... /

which checks the string against the given regular expression. This is one of the most useful features of the Perl language.

More info at http://perldoc.perl.org/perlre.html

(I did use the m'' syntax to improve readability, because of the presence of another '/' character in the regexp. I could also write /\/systemfile\d+\.elf$/

Orabîg
  • 11,718
  • 6
  • 38
  • 58
  • 1
    I agree with using an explicit `m` on the match operator so that you can change the delimiter. But I think that the single quote is a terrible choice of alternative. It makes the regex look like a string. I would have probably gone with `m[systemfile(16|32)\.elf$]`. – Dave Cross Oct 25 '16 at 09:46
0
use strict;
use warnings;

my $string = 'systemfile16.elf';
if ($string =~ /^systemfile.*\.elf$/) {
print "Found string $string";
  } else {
print "String not found";

will match systemfile'anythinghere'.elf if you have a set directory.

if you want to search entire string, including directory then:

my $string = 'c:\\windows\\system\\systemfile16.elf';
if ($string =~ /systemfile.*\.elf$/) {
print "Found string $string";
  } else {
print "String not found";

if you only want to match 2 systemfile then 2 numeric characters .elf then use the other methods mentioned above by other answers. but if you want systemanything.elf then use one of these.

  • 1
    You should escape the last dot, because it is the character dot not the placeholder – Jens Oct 25 '16 at 09:44
  • @Jens. true, I never tested the code. Will update it, although it will work regardless of it being wrong :) so by not escaping the . it will accept systemfile00.self as a match as well, here we want to match exactly .elf only. Thanks for spotting it –  Oct 25 '16 at 09:54
  • Actually, "systemfile.." is at the end of the string, so you don't want that `^` at the start (and you example string should rather be `'/home/me/Desktop/MyWork/systemfile/directory/systemfile09.elf'` for instance). Moreover, `.*` will match anything including strings like `systemfile/not/the/right/one/.elf` ... So definitely not what the OP would like. – Dada Oct 25 '16 at 10:29
  • ok, I missed the part where he wanted the entire string, so we should then drop the ^. However he specifies "substring in the format systemfile*.elf" which means systemfile"anythinghere".elf so could be systemfile16.elf, systemfile32.elf or sytemfile64bit.elf in my view. –  Oct 25 '16 at 10:35
0
if ($string =~ /systemfile.*\.elf/) {
    # Do something with the string.
}

That should match only the strings you seek (given that every time, a given string is stored in $string). Inside the curly brackets you should write your logic.

The . stands for "any character" and the * stands for "as many times you see the last character". So, .* means "any character as many times you see it". If you know that the string will end in this pattern, then it will be safer to add $ at the end of the pattern to mark that the string should end with this:

$string =~ /systemfile.*\.elf$/

Just don't forget to chomp $string to avoid any line-breaks that might mess with your desired output.

yoniyes
  • 1,000
  • 11
  • 23
  • This will also match `*systemfile*.elf*` the OP specifically says he wants to match systemfile*.elf hence why I answered `if ($string =~ /^systemfile.*.\.elf$/);` –  Oct 25 '16 at 10:18
  • and I made the same mistake by assuming he wanted to match the filename only without path. :) –  Oct 25 '16 at 10:40
  • this will match strings with the full path as well – yoniyes Oct 26 '16 at 05:49