0

Hi,

I want to print the word that start with h(not case sensitive).
i tried with below code but its printing only one charater 'H'.
i dont understand the metacharaters and regular expressions much.
i understood that $1 will have the first match and $2 will have second match and so on.
please help me.

$line = "Hello how are you";
  $line =~ m/\b(h*)/i;
  print $1."\n";
3
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by Gaiety
0

Thank you very much for the below code.

$line = "Hello how are you";
while($line =~ m/\b(h\w+)/ig){
print $&."\n";
}

i actually want to understand $1 and $2 variables and the extraction process.
can i have the single expression without while, which extracts the words starts with H/h and returns.

Regards,
gaiety.

Edited by Gaiety: n/a

0

The value of whatever matches what is in the first pair of parentheses goes into $1, and the value of whatever matches what is in the second pair of parentheses goes into $2. Here's an example of capturing matches into the $1 and $2 variables.

#!/usr/bin/perl;
use strict;
use warnings;

my $line = "Hello Harry, how is your house?";

$line =~ m/(\bh\w+).*?(\bh\w+)/i;

print '$1 = ', "$1\n";
print '$2 = ', "$2\n";

Note that it's not as flexible as thines01 script because the pattern will capture only the first to words starting with 'h' or 'H' and ignores the rest.

0

problem is solved.
Thank you very much.

But how to understand the expressions and their traversal and evaluation.
i mean the expression "(\bh\w+).*?(\bh\w+)/i;"

i know \b means word boundary i.e find words only
h specifies words start with h
\w specoifies any alphanumeric

what this ".*? " is for i dont understand.

is there any web source where i can find the meaning of expressions and their examples.

0

Thank you a lot.

i will go through the links.

Regards,
Gaiety.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.