1

Is there a way to replace characters from inside the regex?

like so:

find x | xargs perl -pi -e 's/(as dasd asd)/replace(" ","",$1)/'

From OP's comment

code find x | xargs perl -pi -e 's/work_search=1\/ttype=2\/tag=(.*?)">(.*?)<\/a>/work\/\L$1\E\" rel=\"follow\">$2<\/a>/g'

in this case i want $1's spaces be replaced with _

0

2 Answers 2

2

You can use a nested substitution:

$ echo 'foo bar baz' | perl -wpE's/(\w+ \w+ \w+)/ $1 =~ s# ##gr /e'
foobarbaz

Note that the /r modifier requires perl v5.14. For earlier versions, use:

$ echo 'foo bar baz' | perl -wpE's/(\w+ \w+ \w+)/my $x=$1; $x=~s# ##g; $x/e'
foobarbaz

Note also that you need to use a different delimiter for the inner substitution. I used #, as you can see.

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

2 Comments

This answer could be improved by adding spaces around the "=~"
@hochgurgler Perhaps readability could be improved, but I detest one-liners that are so long that they trigger the scrollbar.
0

As far as I understand, you want to remove the spaces. Is it correct?

You can do:

s/(as) (dasd) (asd)/$1$2$3/

3 Comments

correct, but the "(as dads asd)" is just a part of the regex - sorry for confusion, but i didn't post the entire regex: code find x | xargs perl -pi -e 's/work_search=1\/ttype=2\/tag=(.*?)">(.*?)<\/a>/work\/\L$1\E\" rel=\"follow\">$2<\/a>/g' in this case i want $1's spaces be replaced with _
<a href="/work_search=1/ttype=2/tag=S & A Homes">S & A Homes</a> should become <a href="/work/s_&_a_homes">S & A Homes</a>
@user845435: Please edit you question instead of add comment with code.

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.