Help to fetch first two characters from a word in perl

Hi All,
I have a word "DE_PR_Package__Basic" , i need to check if the first two characters of this words is DE or something else using perl script.
Can anyone pls let me know how to proceed?

Thanks in advance.
Giri!

Depends on how you want to proceed with that information. To just print the line if a word matches your requirements:

perl -ne 'print if /\bDE_[\w_]+?/;' input.txt

If you want just the remainder of the word:

perl -ne 'print $1 if /\bDE_([\w_]+?)/' input.txt
1 Like

check if this is what you are looking for:

$word = "DE_PR_Package__Basic";
if ($word=~ m/^DE/) {
    print "matches\n";
}
else {
    print "does not match\n";
}

in the regex, ^ is the start of string. You might want to add _ after DE so that the regex is ^DE_

1 Like

Thanks a lot Pludi, it was very useful for me.

---------- Post updated at 04:46 AM ---------- Previous update was at 04:45 AM ----------

Thanks a lot Yogesh, It was very useful for me.