1751922 Members
4963 Online
108783 Solutions
New Discussion юеВ

Perl question

 
SOLVED
Go to solution
Kris_5
Occasional Advisor

Perl question

Dear All,

I am trying to search for a string and replace with another in a perl script. The search string can be "clin" or "clin2" and replace string is "clin2".

Here is the example
$repl_str=clin2
$dir=/opt/data/clin/0001/view
# Search for clin? and replace clin with the replace string.
$dir =~ s/clin?/$repl_str/g;
### resulting value of $dir is /opt/data/clin2/0001/view which is O.K.

But it fails for scenario 2.
$repl_str=clin2
$dir=/opt/data/clin2/0001/view
# Search for clin? and replace clin with the replace string.
$dir =~ s/clin?/$repl_str/g;
### resulting value is $dir=/opt/data/clin22/0001/view,
but I expected the value as
/opt/data/clin2/0001/view.

Could some body help in modifying the search command. TIA

Kris
2 REPLIES 2
curt larson_1
Honored Contributor

Re: Perl question

the n? in clin? will match zero or 1 n.
that means it will match cli and clin.
your pattern should be clin.?
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Perl question

not clin.? but clin2?

the . will match anything but a newline, which also includes slashes, which is not what you want. 2? means zero ore one literal 2

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn