Search code examples
regexregex-lookaroundsregexp-replace

RegEx Help. Using lookaround to insert periods inbetween spaced digits, but only if the digit isn't at the very end of a word


Examples:

RP Distort2 1 0 0b.exe
AFakeFilename4 5 0 2 SEP2 5 63 8
A4 5 8 7 6 COM99 6 4 4 1

Should become:

RP Distort2 1.0.0b.exe
AFakeFilename4 5.0.2 SEP2 5.63.8
A4 5.8.7.6 COM99 6.4.4.1

My current expression is:

(?<=\d) (?=\d)

Replacement: .

Right now my expression only partially works. It inserts periods between all spaced digits. I.E: RP Distort2 1 0 0b.exe becomes RP Distort2.1.0.0b.exe when it should be RP Distort2 1.0.0b.exe

I am not a RegEx wizard so this has me kind of stumped. I also got my expression from another site - I know it's using lookaround but I don't exactly understand the syntax of lookaround.

I'm using this expression in den4b Renamer (beta3) which now supports lookaround fully.

How can I modify my expression to achieve what I need here?

Any help at all would be enormously appreciated.

Edit:

I also want to add an additional condition. If a digit is immediately after a single v I.E: TestSoftware v1 5 5, this should not count as "at the end of a word" (an exception) and the result should be TestSoftware v1.5.5.


Solution

  • Converting my commemt to an answer for future readers.

    Lookarounds do not seem to be implemented, you could run for example this pattern twice:

    \b(v?\d+) (\d)
    

    In the replacement use $1.$2