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
Reply to #1:
Hi DF,
You can do it with a regular expression.
First, we need to capture all the characters:
(\w) - this captures each character. But we want to insert the letter “a” before every character and stop when we reach the comma. We can achieve that using a positive lookahead:
(\w)(?=(\w)*,)
So the replacement would be:
Replace: (\w)(?=(\w)*,)
Replace with: a\1
Miguel
Hi DF,
You can do it with a regular expression.
First, we need to capture all the characters:
(\w) - this captures each character. But we want to insert the letter “a” before every character and stop when we reach the comma. We can achieve that using a positive lookahead:
(\w)(?=(\w)*,)
So the replacement would be:
Replace: (\w)(?=(\w)*,)
Replace with: a\1
Miguel
Reply to #5:
Leave it to Miguel! : )
I KNEW one of you guys would have the answer... that's some mighty sweet regex-ing my friend!
I thought I'd tried every permutation of lookahead, but obviously not.
MIGUEL for the GOOOOOOOAAAAALLLLL!!!!!
Thanks man, that's a load off my poor old brain...
You are the best!
DF
Leave it to Miguel! : )
I KNEW one of you guys would have the answer... that's some mighty sweet regex-ing my friend!
I thought I'd tried every permutation of lookahead, but obviously not.
MIGUEL for the GOOOOOOOAAAAALLLLL!!!!!
Thanks man, that's a load off my poor old brain...
You are the best!
DF
Reply to #6:
BTW, this wasn't *just* a thought experiment. I was looking for ways to escape, that is precede with a backslash, non-word characters in various strings. It's useful in a number of situations, like for instance https://www.advancedrenamer.com/forum_thread/another-way-to- rename-with-text-files-easy-script-16414
Search: ([^\w])(?=.*,)
Replace: \\\1
...works perfectly. Thanks again for getting me out of my brain-lockup, Miguel.
BTW, this wasn't *just* a thought experiment. I was looking for ways to escape, that is precede with a backslash, non-word characters in various strings. It's useful in a number of situations, like for instance https://www.advancedrenamer.com/forum_thread/another-way-to- rename-with-text-files-easy-script-16414
Search: ([^\w])(?=.*,)
Replace: \\\1
...works perfectly. Thanks again for getting me out of my brain-lockup, Miguel.
Reply to #5:
Nice!
Or simplified to (\w)(?=\w*,)
I didn't even consider regex for this since DF seemed to have switched to JS. ;)
Nice!
Or simplified to (\w)(?=\w*,)
I didn't even consider regex for this since DF seemed to have switched to JS. ;)
Reply to #8:
I'm very versatile! LOL
I'm very versatile! LOL