#1 : 13/06-25 00:12 Tavo
Posts: 10
|
Hello again to this wonderful community!
Can someone pinpoint me how to change or replace a slash - for a dot . ?? Here's an example: from this: v1-4-57 to this: v1.4.57 It only needs to apply to slashes between consecutive numbers, not letters! Thanks in advance! |
#2 : 13/06-25 01:54 Delta Foxtrot
Posts: 500
|
Reply to #1:
Hi Tavo, glad you like it here! :) If you are using the latest version, 4.12, you can use regex lookahead to find all numbers followed by a dash and then followed by another number: REPLACE method: Replace: (\d)-(?=\d) Replace with: $1. Occurrence: All Use regular expressions CHECKED Apply to: Name This finds the number followed by a dash. When it finds that the regex engine "peeks" ahead to the next character to determine if it's also a number - but that number is not included in the actual expression if if finds one. You need lookahead here because otherwise the regex engine won't find the second dash, so if you used a replace method with "(\d)-(\d)" replaced by "$1.$2" you'll only replace the first occurrence (no matter that the "Occurrence:" is set to "All"). That's because the regex engine is position-based; after it's tested the first <digit><dash><digit> combination it is already positioned after the second digit, so the next character is a dash and it won't find your combination, even though it seems like it should. If the filename were "v1-44-57" it would find both occurrences. Whereas a lookahead does not "consume" the last character (the lookahead digit), so it's still there for the regex engine to "find". Weird, huh? Best, DF |
#3 : 13/06-25 03:58 GSComputer
Posts: 42
|
Reply to #! #2:
@Tavo My easy solution REPLACE method: Replace: - Replace with: . Occurrence: All Use regular expressions CHECKED Apply to: Name @Delta Foxtrott Thanks for solution I apreciate Your Solution Method "Replace: (\d)-(?=\d)" and your explanation to it. That gives me new ideas for my having and maybe future problems ;-) ((?=\d) totally new for me. I have to read about the lookahead/lookbehind possibilities. |
#4 : 13/06-25 05:18 Delta Foxtrot
Posts: 500
|
Reply to #3:
Hi GS, it's been a while my friend! The reason I used lookahead is Tavo said: > It only needs to apply to slashes between consecutive numbers, not letters! Lookahead is the way to do that. It's lucky he asked today because version 4.12 just came out, with an improved regex engine that I discovered would do lookahead; before this version it wasn't possible in ARen. And "lookaround", the generalized term for ahead/behind, is a very versatile problem solver (although I haven't tried lookbehind yet, but I'll see if it's implemented soon). Anyway, good to see you again. With this new version we may be able to solve some other problems? :) Best DF |
#5 : 14/06-25 00:21 Delta Foxtrot
Posts: 500
|
Reply to #4:
To anyone interested: Lookahead and negative lookahead seem to work as expected. Lookbehind (both forms) uses a "<" and ARen seems to think it's a malformed tag, so that's a no. Named capture groups works as expected, and could be useful in the case of complex regex. Recursion pattern (?R) seems to work and relative recursion (?1) seems to work, although I haven't tried something like (?-1) yet, mainly because my simple regex use hasn't called for it. I do wish lookbehind worked, that can be very handy... Oh, by the way, I think there have been some versions of ARen before 4.12 that would do lookahead, but I always avoided it so I'm not even sure. Just a thought, as I previously said that no other version would do it. Best, DF |
#6 : 14/06-25 05:21 Tavo
Posts: 10
|
Hello DF!
Thanks for the heads-up on the new v4.12, that's great news. Indeed I was lucky the RegEx engine was improved these days my problem arose. I was running crazy relying on the good ol' lookaround solution to no avail. To everyone: The new Lookahead solution is working with full versatility at least on my given example, go ahead with confidence and try your own. --- So, all good now ***SOLVED*** Thank you both @DF and @GSComputer for your solutions. |
#7 : 14/06-25 06:13 Delta Foxtrot
Posts: 500
|
Reply to #6:
Hi Tavo, Glad we could fix your problem buddy! As Kim would say, "Fun fact:" The javascript engine can use both forward and backward lookaround, positive and negative. So the same problem can be solved using a script with this wording: name1 = item.newBasename ; name2 = "" ; pattern = /(?<=\d)-(?=\d)/g ; name2 = name1.replace( pattern, "." ) ; return name2 ; Using a regex pattern like: pattern = /(?<![A-Za-z])-(?![A-Za-z])/g ; (negative lookaround) also works. Screenshot: https://drive.google.com/file/d/1mY4f2gEFZ8cVTSL i_Lv82k3cBTXYbvJ5/view?usp=sharing For such a simple problem there's no need to use that, but it's good to know it's there if I need it. I'm going to have to test for other regex diamonds buried in the script... Something else I've never even thought about trying. Best, DF |