0

I need to search in a long text, the lines containing text like "candidate no:23."

the line must have a dot in the end. I have tried

$pat = 'candidate no:??'

if ($line =~ /$pat/) {
print "match found \n";
}

This works fine, but not when I try include dot in my pattern

$pat = 'candidate no:??.'

or

$pat = 'candidate no:??\.'

Also tried

if ($line =~ /${pat}\.$/) 

None of the above work, can anyone suggest a way out. The pattern must match string "candidate no:(some number)." with a dot and then end of line.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • `:??` matches `:` zero or one times, but as few as possible. `.` matches any character (not just dot). `:??\.` matches `.` or `:.` literally. You never specified a match for numbers or whatever comes after `:` and before `.`. – ctwheels Nov 16 '19 at 00:50
  • "_match string "candidate no:(some number)." with a dot and then end of line._" -- so you've very nicely articulated a description of a regex pattern, next would be to go and figure out how to do it. Now you've got a solution, but I still suggest to go over the tutorial [perlretut](https://perldoc.perl.org/perlretut.html) with the reference [perlre](https://perldoc.perl.org/perlre.html) by your side – zdim Nov 16 '19 at 02:41

3 Answers3

2

The pattern must match string "candidate no:(some number)." with a dot and then end of line.

I think that the issue comes from the double question mark in your regex. ?? is a lazy regexp quantifier, which represents 0 or 1 occurences of the preceeding element (here, the semi colon ':'). This does not really make sense, given the above specification.

Your regexp is missing a representation of the (integer) number. For this, you can use '\d+':

$pat = 'candidate no:\d+\.'

Update after the comment by zdim

Here is an alternative solution that would optionaly accept spaces around the number, and explicitly defines the end of the string after (optional spaces and) the number.

$pat = 'candidate no:\s*\d+\s*\.\s*$'
GMB
  • 216,147
  • 25
  • 84
  • 135
  • 1
    Ironically, matching with dot is exactly what the OP needed to do. `.` is what one would use to match any character, so the OP wanted `candidate no:..\.`. Your versions are improvements, though. – ikegami Nov 16 '19 at 02:41
0

You need do some reading on regex to get better understanding of patterns

use strict;
use warnings;

my $match = qr/candidate no:\d+\./;

while( <DATA> ) {
    print if /$match/;
}

__DATA__
candidate no:2
candidate no:12
candidate no:43.
candidate no:254
candidate no:122.
candidate no:9.
candidate no:564
candidate no:367.
candidate no:98.
candidate no:25
candidate no:79

Perl regex, Perl regex, Perl regex, Perl regex, Mastering Regular Expressions

Polar Bear
  • 6,762
  • 1
  • 5
  • 12
0

As alternative the code can be changed to

use strict;
use warnings;

my $search = qr/candidate no:\d+\./;

map { print if /$search/ } <DATA>;

__DATA__
candidate no:2
candidate no:12
candidate no:43.
candidate no:254
candidate no:122.
candidate no:9.
candidate no:564
candidate no:367.
candidate no:98.
candidate no:25
candidate no:79
Polar Bear
  • 6,762
  • 1
  • 5
  • 12