Gokul.Mohan
Gokul.Mohan

Reputation: 39

How to replace some patterns in a file line with white spaces in perl?

I'm having a file, with some content, let's say, press y ='Accept' press y ='hey there'

i need to clear all those patterns with y ='something' ? How to do this using perl ?

In the past, I've tried like

foreach(@lines) {
     $_ =~ s/y='*/ /g;
    }

but this is only replacing a single character after the pattern

I'm not sure about the following pattern or length of the following pattern, but need to replace every thing after the 'y= pattern ? provided y= is not at the starting of the line

Upvotes: 1

Views: 63

Answers (1)

Anirudh Simha
Anirudh Simha

Reputation: 360

This is what I've tried and is working for me

#!/usr/bin/perl

my $str = "press y ='Accept' press y ='hey there'";
$str =~s/y ='.*?'/ /g;
print $str;

What this does is matches from "y='" to first matching single quote after that i.e Its a non greedy matching regex.

Upvotes: 2

Related Questions