Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find a substring within a string using Perl?

I have a string from which I wish to extract a single word, but with a numerical appended to it, which might be different in each line:

This is string1 this is string
This is string11 
This is string6 and it is in this line

I want to parse this file and get the values of "stringXXX", starting from 0 to 100

# suppose ABC.txt contains the above lines
FH1 = open "Abc.txt"; 
@abcFile = <FH1>;

foreach $line(@abcFile) {
    if ($pattern =~ s/string.(d{0}d{100});
        print $pattern;

The above prints the whole line, I wish to get only stringXXX

like image 400
gagneet Avatar asked Dec 08 '08 04:12

gagneet


People also ask

How do I find a substring in a string in Perl?

To search for a substring inside a string, you use index() and rindex() functions. The index() function searches for a substring inside a string from a specified position and returns the position of the first occurrence of the substring in the searched string.

How do I find a substring inside a string?

Run a loop from start to end and for every index in the given string check whether the sub-string can be formed from that index. This can be done by running a nested loop traversing the given string and in that loop running another loop checking for sub-strings starting from every index.

What does =~ do in Perl?

The operator =~ associates the string with the regex match and produces a true value if the regex matched, or false if the regex did not match.


2 Answers

you need to capture it:

while ($pattern =~/(string(100|\d{1,2}))/g) {
    print $1;
}

Explanation:

  • the parentheses capture what's in them into $1. If you have more than one set of parens, the 1st captures into $1, the 2nd into $2 etc. In this case $2 will have the actual number.
  • \d{1,2} captures between 1 and 3 digits, allowing you to capture between 0 and 99. The additional 100 there allows you to capture 100 explicitly, since it's the only 3-digit number you want to match.

edit: fixed the order of the numbers that are captured.

like image 181
Nathan Fellman Avatar answered Oct 05 '22 18:10

Nathan Fellman


Abc.pl:

#!/usr/bin/perl -w    
while(<>) {
    while (/(string(\d{1,3}))/g) {      
    print "$1\n" if $2 <= 100;
    } 
}

Example:

$ cat Abc.txt 
This is string1 this is string
This is string11 
This is string6 and it is in this line
string1 asdfa string2
string101 string3 string100 string1000
string9999 string001 string0001

$ perl Abc.pl Abc.txt
string1
string11
string6
string1
string2
string3
string100
string100
string001
string000

$ perl -nE"say $1 while /(string(?:100|\d{1,2}(?!\d)))/g" Abc.txt
string1
string11
string6
string1
string2
string3
string100
string100

Note the difference between the outputs. What is preferable depends on your needs.

like image 40
jfs Avatar answered Oct 05 '22 19:10

jfs