Unitarihedron
Unitarihedron

Reputation: 203

Removing characters using RegExp Perl

I'm trying to remove characters using regular expressions in Perl. After showing :set list on vim, the characters, with formatting are shown below:

R$
$
e$
a$
$
di$
n$
U$

This substitution works, but it is too broad for my purposes:

$text =~ s/\w{1,2}//g;

The substitution I would like to make is:

$text =~ s/\w{1,2}$//g;

However, that does not remove any of the below characters. I am confused as to why this is. I'm wondering whether this has to do with possible Unicode characters; if so, how do I see and remove them?

Upvotes: 1

Views: 97

Answers (2)

hmatt1
hmatt1

Reputation: 5129

The problem here is that you need to use the m modifier to treat the regex as multiple lines. Here is the documentation.

$text =~ s/\w{1,2}$//mg;

Here is an example that I copied and pasted from vim with :set list

#!/bin/perl$
use strict;$
use warnings;$
$
my $text = <<EOD;$
R$
$
e$
a$
$
di$
n$
U$
EOD$
$
$text =~ s/\w{1,2}$//gm;$
$
print $text;$

The output is a bunch of blank lines.

Upvotes: 1

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

$ is an anchor character in regex which means end of string or line(similar to \Z). You need to escape the $ with \

$text =~ s/\w{1,2}\$//g;

In your input I have seen you are also removing only $ character. In that case you have to make the \w{0,2}, means any alphanumeric with underscore(_) having 0 to 2 in lengths.

Upvotes: 2

Related Questions