简体   繁体   中英

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. IE: 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 IE: 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 .

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

This would do the substitutions you want from the example strings you have in your question

Match and capture:

(?:(?<=\s)|(?<=\sv))(\d+)\s(?=\d)

Substitution:

$1.
  • (?: - start of non-capturing group
    • (?<=\s) - positive lookbehind for a whitespace
    • | - OR
    • (?<=\sv) - positive lookbehind for a whitespace + v
  • ) - end of non-capturing group
  • (\d+)\s - match and capture 1 or more digits followed by a whitespace
  • (?=\d) - positive lookahead for a digit

Demo

Try \b\d+\K (?=\d) if your regex supports the \K feature, what I call the poor man's variable lookbehind. It discards the entire match to that point.

Regex101 on PCRE regex, 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM