bazzilic
bazzilic

Reputation: 828

How to match word surronded by spaces OR at the end / beginning of the string using Perl regexp?

I want to find sequences matching my regexp should they be in the middle of the string surrounded by spaces, in the end or beginning or be the only thing in a string.

Example: Let's assume the sequence 'qwe45rty' is what we are looking for. I want to be able to get positive on all of these strings:

'qwe45rty' 'qwe45rty blabla' 'smth qwe45rty blabla' 'smth qwe45rty' ' qwe45rty '

But none of these:

'aaqwe45rty' 'qwe45rtybb' 'aaqwe45rtybb'

Best what I came up with is smth like this:

if ( ($a =~ /\s+$re\s+/) or
     ($a =~ /^$re\s+/)   or
     ($a =~ /\s+$re$/)   or
     ($a =~ /^$re$/)        )
{
    # do stuff
}

which can't be the best way to do that :)

Any suggestions?

Upvotes: 12

Views: 17607

Answers (3)

AndreKR
AndreKR

Reputation: 33678

You can do the or inside the regex:

/(^|\s+)qwe45rty(?=\s+|$)/

regex101

Note that the second group is a positive lookahead (?=) so it checks for whitespace, but doesn't consume it. That way the regex can match two consecutive occurrences of the string and give an accurate match count.

Upvotes: 27

Alan Moore
Alan Moore

Reputation: 75222

Try coming at the problem from a different direction. To say something can match whitespace or nothing is to say it can't match a non-whitespace character:

(?<!\S)qwe45rty(?!\S)

Just a little shift in perspective and the regex practically writes itself.

Upvotes: 9

Kenosis
Kenosis

Reputation: 6204

Try the following:

$a =~ /(?:\A|\s)$re(?:\s|\Z)/;

For example:

use strict;
use warnings;

my $re = 'qwe45rty';
while (<DATA>) {
    chomp;
    print "'$_': Match? " . ( /(?:\A|\s)$re(?:\s|\Z)/ ? 'Yes' : 'No' ) . "\n";
}

__DATA__
qwe45rty
qwe45rty blabla
smth qwe45rty blabla
smth qwe45rty
 qwe45rty 
aaqwe45rty
qwe45rtybb
aaqwe45rtybb

Output:

'qwe45rty': Match? Yes
'qwe45rty blabla': Match? Yes
'smth qwe45rty blabla': Match? Yes
'smth qwe45rty': Match? Yes
' qwe45rty ': Match? Yes
'aaqwe45rty': Match? No
'qwe45rtybb': Match? No
'aaqwe45rtybb': Match? No

Upvotes: 1

Related Questions