1

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?

2 Answers 2

2

$ 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.

Sign up to request clarification or add additional context in comments.

1 Comment

$ is not the actual $ character. It is a construct of :set list on vim, which shows formatting.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.