Inert "a" before *each* character up to a comma
Hello friends,
[EDIT: The title should start with "Insert", not "Inert"... END EDIT]
As an experiment I had filenames like:
H12577,883665717
T66635,252583321
Now, I want to insert another character (in this case "a") in front of EACH character up to (but not including) the comma.
Files should then look like:
aHa1a2a5a7a7,883665717
aTa6a6a6a3a5a2,252583321
Seems simple, right? Now I may just be groggy from not enough coffee yet this morning, but for the life of me I can't think of a way to do this in regex. It's easy--if counterintuitive--to put an "a" in front of all characters:
Replace: ((.)) With: a$2 Occurr: All Regex: yes
and to remove it in front of the comma (R: a, W: ,), but I'm stuck on how to remove the "a"s.
Maybe recursion, which I know nothing about.??
The original problem is super simple in javascript, (split before comma, make an array of characters from the first part, add an "a" to each array element, rejoin the parts [one of many ways]) but I'd love to know how to do it in regex, especially in one regex Replace method.
Bonus points for how to remove the "a"s after they've been inserted! :)
Thanks folks!
Best,
DF
[EDIT: The title should start with "Insert", not "Inert"... END EDIT]
As an experiment I had filenames like:
H12577,883665717
T66635,252583321
Now, I want to insert another character (in this case "a") in front of EACH character up to (but not including) the comma.
Files should then look like:
aHa1a2a5a7a7,883665717
aTa6a6a6a3a5a2,252583321
Seems simple, right? Now I may just be groggy from not enough coffee yet this morning, but for the life of me I can't think of a way to do this in regex. It's easy--if counterintuitive--to put an "a" in front of all characters:
Replace: ((.)) With: a$2 Occurr: All Regex: yes
and to remove it in front of the comma (R: a, W: ,), but I'm stuck on how to remove the "a"s.
Maybe recursion, which I know nothing about.??
The original problem is super simple in javascript, (split before comma, make an array of characters from the first part, add an "a" to each array element, rejoin the parts [one of many ways]) but I'd love to know how to do it in regex, especially in one regex Replace method.
Bonus points for how to remove the "a"s after they've been inserted! :)
Thanks folks!
Best,
DF
Reply to #1:
...and when I say it's easy in javascript, I mean it's *this* easy:
name = item.newBasename ;
first = name.substring( 0, name.indexOf(",") );
last = name.substring( name.indexOf(",") ) ;
firstArr = first.split( "" ) ;
for (j = 0; j< firstArr.length; j++ ) {
firstArr[j] = "a" + firstArr[j] ;
}
return firstArr.join("") + last ;
...and when I say it's easy in javascript, I mean it's *this* easy:
name = item.newBasename ;
first = name.substring( 0, name.indexOf(",") );
last = name.substring( name.indexOf(",") ) ;
firstArr = first.split( "" ) ;
for (j = 0; j< firstArr.length; j++ ) {
firstArr[j] = "a" + firstArr[j] ;
}
return firstArr.join("") + last ;
Reply to #2:
With the more modern JS of Aren 4, it should be even easier:
sepStr = "a";
[firstPart, LastPart] = item.newBasename.split (",");
newfirstPart = sepStr + ( firstPart.split("") ).join (sepStr);
newName = `${newfirstPart},${LastPart}`;
return newName;
Although if there is more than one comma, things get more fun. ;)
PS: There are many, many occasions when the temptation to use regex will lead only to ruin and madness... EG: https://stackoverflow.com/a/1732454
With the more modern JS of Aren 4, it should be even easier:
sepStr = "a";
[firstPart, LastPart] = item.newBasename.split (",");
newfirstPart = sepStr + ( firstPart.split("") ).join (sepStr);
newName = `${newfirstPart},${LastPart}`;
return newName;
Although if there is more than one comma, things get more fun. ;)
PS: There are many, many occasions when the temptation to use regex will lead only to ruin and madness... EG: https://stackoverflow.com/a/1732454
Reply to #3:
Hi Randy,
...or even:
[firstPart, LastPart] = item.newBasename.split (",");
newfirstPart = "a" + ( firstPart.split("") ).join("a");
return `${newfirstPart},${LastPart}` ;
...or even:
[firstPart, LastPart] = item.newBasename.split (",");
return "a" + ( firstPart.split("") ).join("a") + `,${LastPart}` ;
... but I always opt for readability (and I'm not that good a javascript coder to begin with, so I try to stay away from stuff that numbs my dog-brain :) But as I said in my original post, there are lots of ways to do it in javascript. EDIT: My code always looks more like PetBASIC or DBASE II than an object language. END EDIT
Good work, and that's a great link. One could probably say the same about optimizing javascript, though... :)
Best,
DF
Hi Randy,
...or even:
[firstPart, LastPart] = item.newBasename.split (",");
newfirstPart = "a" + ( firstPart.split("") ).join("a");
return `${newfirstPart},${LastPart}` ;
...or even:
[firstPart, LastPart] = item.newBasename.split (",");
return "a" + ( firstPart.split("") ).join("a") + `,${LastPart}` ;
... but I always opt for readability (and I'm not that good a javascript coder to begin with, so I try to stay away from stuff that numbs my dog-brain :) But as I said in my original post, there are lots of ways to do it in javascript. EDIT: My code always looks more like PetBASIC or DBASE II than an object language. END EDIT
Good work, and that's a great link. One could probably say the same about optimizing javascript, though... :)
Best,
DF