rgvcorley
rgvcorley

Reputation: 2933

Is is possible to get a single regex to replace 'str1/str2' with 'str1/str2,str1->str2'

The Problem

I have a load of strings in this form:-

str1/str2
str3/str4/str5

There can be any number of segments. I would like to do a regex search and replace to get the output:-

str1/str2 , str1->str2
str3/str4/str5 , str3->str4->str5

My strings are actually in the form [a-z|_]+

My attempt

Match with:-

((?:(?:[a-z|_])\/?)+)

Replace with:-

$1 , $1

This is nearly right, but I'd need to do a second search and replace to change the /'s to ->'s.

Can I achieve this with a single regex? Or does it depend on the engine I'm using and it's backreference capabilities? (I'm using the search and replace functionality in aptana).

Upvotes: 1

Views: 584

Answers (1)

Mitya
Mitya

Reputation: 34586

Rather than a repeating group does Aptana support a global modifier? This works in JavaScript:

"str3/str4/str5/str6/str7".replace(/([a-z0-9_|]+)\//g, '$1->');
//output: str_3->str_4->str_5->str_6->str_7

Also, I guess you meant your pattern should also allow numbers.

Upvotes: 2

Related Questions