Rudra
Rudra

Reputation: 21

searching a word with a particular character in it in perl

am trying to search a word where it starts with any character (Capital letter) but ends with zero in perl. For example

ABC0
XYZ0
EIU0
QW0

What I have tried -

$abc =~ /^[A-Z].+0$/

But I am not getting proper output for this. Can anybody help me please?

Upvotes: 0

Views: 59

Answers (2)

amon
amon

Reputation: 57640

The ^ anchores at the start of a string, the $ at the end. .+ matches as many non-newline-characters as possible. Therefore

"ABC0 XYZ0 EIU0 QW0" =~ /^[A-Z].+0$/

matches the whole string.

The \b assertion matches at word edges: everywhere a word character and a non-word-character are adjacent. The \w charclass holds only word characters, the \S charclass all non-space-characters. Either of these is better than ..

So you may want to use /\b[A-Z]\W*0\b/.

Upvotes: 1

Sidharth C. Nadhan
Sidharth C. Nadhan

Reputation: 2253

This might work :

$abc =~ /\b[A-Z].*0\b/

\b matches word boundaries.

Upvotes: 0

Related Questions