简体   繁体   中英

Selecting all zeroes before a number using regex

I'm trying to usePowerRename , which supports using regex to select parts of filenames for replacement. I'm trying to use that to clean up some filenames by deleting the first few zeroes before a number in a file name (replacing them with nothing).

Here are some examples of what the files are named like, what the pattern used should match, and what PowerRename shows as the preview for what the file name will be when the matched string in the file name gets replaced with nothing.

filename -> matching string -> renamed file
00000.jpg -> "0000" -> 0.jpg
00001.jpg -> "0000" -> 1.jpg
00010.jpg -> "000"  -> 10.jpg
00011.jpg -> "000"  -> 11.jpg
00100.jpg -> "00"   -> 100.jpg
00101.jpg -> "00"   -> 101.jpg
00219.jpg -> "00"   -> 219.jpg
01000.jpg -> "0"    -> 1000.jpg
12345.jpg -> ""     -> 12345.jpg
img.00002.jpg -> "0000" -> img.2.jpg

What regex pattern can I use to accomplish this? I've tagged this as .NET because that's the regex documentation I got redirected to from within the app.

I've tried various combinations but the closest I got was [0]{2} , which correctly handles 1 digit numbers but incorrectly handles and number that's 2 digits or anything 3+ digits long with multiple zeros. (20, 100, etc.)

Here's an example of what the pattern [0]{2} does when used:

filename -> renamed file
00000.jpg -> 0.jpg
00001.jpg -> 1.jpg
00010.jpg -> 010.jpg
00011.jpg -> 011.jpg
00100.jpg -> 1.jpg
00101.jpg -> 101.jpg
00219.jpg -> 219.jpg
01000.jpg -> 010.jpg
12345.jpg -> 12345.jpg
image.00002.jpg -> image.2.jpg

I've also tried [^0][0-9]* , which replaces everything but the zeroes and only works if the file name (excluding file extension) contains only numbers:

filename -> renamed file
00000.jpg -> 00000
00001.jpg -> 0000
00010.jpg -> 000
00011.jpg -> 000
00100.jpg -> 00
00101.jpg -> 00
00219.jpg -> 00
01000.jpg -> 0
12345.jpg ->
image.00002.jpg ->

You can use a word boundary to prevent a partial match, then match one or more times a zero asserting a digit to the right:

\b0+(?=[0-9])

See a regex101 demo .

For the example data, you might also use \B at the end instead of a lookahead assertion:

\b0+\B

See another regex101 demo .

Scan with (?<?[0-9])0+(?=[0-9]) and replace with nothing. Here it is in Regex101: 在此处输入图像描述

By the way, the documentation for PowerRename says "PowerRename uses the ECMAScript grammar", so .NET is an incorrect tag. Javascript is the correct tag for this utility.

EDIT If you have a problem with lookarounds, you can always include the delimiters as capture groups in the regex and restore them in the replace, like this:

(^|\D)0+(\d)

and replace with $1$2 to restore the two capture groups.

You do have to fiddle with the first capture group to handle the case when file names come right at the beginning (which your example does not have).

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-2025 STACKOOM.COM