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";
}
5
  • 1
    $string =~ /systemfile.*\.elf/ and you should check out perldoc.perl.org/perlre.html Commented Oct 25, 2016 at 8:35
  • Yea that solved It, thanks Commented Oct 25, 2016 at 9:02
  • can you post it as answer, so that i can put correct answer Commented Oct 25, 2016 at 9:02
  • There you go, posted Commented Oct 25, 2016 at 10:04
  • and what if a file exist called 64bitsystemfile.elfde? you will still get a match for systemfile and .elf Commented Oct 25, 2016 at 10:44

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

@HariprasadCR: The index function searches for one string within another, but without the wildcard-like behavior of a full regular-expression pattern match
@HariprasadCR So for me it sounds like you have to do two separate checks if you want to use index
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$/

1 Comment

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$].
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.

3 Comments

This will also match *systemfile*.elf* the OP specifically says he wants to match systemfile*.elf hence why I answered if ($string =~ /^systemfile.*.\.elf$/);
and I made the same mistake by assuming he wanted to match the filename only without path. :)
this will match strings with the full path as well
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.

4 Comments

You should escape the last dot, because it is the character dot not the placeholder
@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
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.
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.

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.