XP is just a number PerlMonks

### Re: replace at the begining

by Melly (Hermit)
 on Aug 20, 2009 at 10:48 UTC ( #790051=note: print w/ replies, xml ) Need Help??

in reply to replace at the begining

The easiest way is to use a negative class (as noted below by others, this will only work when the error isn't the first thing on the line).

```s/[^<]p\sclass=/<p\sclass=/;

Alternatively, use a negative look-behind - (?<!foo):

```s/(?<!<)p\sclass=/<p class=/;

Fixed the code - no \s as a replacement

```map{\$a=1-\$_/10;map{\$d=\$a;\$e=\$b=\$_/20-2;map{(\$d,\$e)=(2*\$d*\$e+\$a,\$e**2
-\$d**2+\$b);\$c=\$d**2+\$e**2>4?\$d=8:_}1..50;print\$c}0..59;print\$/}0..20
Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk
Comment on Re: replace at the begining
Replies are listed 'Best First'.
Re^2: replace at the begining
by \$self (Friar) on Aug 20, 2009 at 11:39 UTC

The first solution doesn't work, e.g. when the problem is at the beginning of a line.

The negative look-behind does, but character classes don't belong in the replacement string- you can just type the space:

```s/(?<!<)p\sclass=/<p class=/;
Re^2: replace at the begining
by Taulmarill (Deacon) on Aug 20, 2009 at 11:32 UTC
You can't use character classes like \s in the replace string, only in the search pattern.
Re^2: replace at the begining
by Anonymous Monk on Aug 20, 2009 at 11:07 UTC
```s/(?<!<)p\sclass=/<p\sclass=/;
Using of space as '\s' why s is displayed between p and class
```<psclass="Hi">
<p class="new">
My mistake - \s means a whitespace character, which can be several things (a space, a tab, etc.). So it makes no sense as a substitution character (of the various options, which should perl use?).
```map{\$a=1-\$_/10;map{\$d=\$a;\$e=\$b=\$_/20-2;map{(\$d,\$e)=(2*\$d*\$e+\$a,\$e**2
-\$d**2+\$b);\$c=\$d**2+\$e**2>4?\$d=8:_}1..50;print\$c}0..59;print\$/}0..20
Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk

Create A New User
Node Status?
node history
Node Type: note [id://790051]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2016-05-05 07:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What font do you use for programming?

Results (90 votes). Check out past polls.