简体   繁体   English

Java中的特殊正则表达式语法

[英]Special Regular Expression syntax in Java

I am using a regular expression for image file names. 我正在为图像文件名使用正则表达式。 The main reason why I'm using RegEx's is to prevent multiple files for the exact same purpose. 我使用RegEx的主要原因是为了防止多个文件完全相同的目的。

The syntax for the filenames can either be: 文件名的语法可以是:

1)    img_0F_16_-32_0.png
2)    img_65_32_x.png

As you might have noticed, "img_" is the general prefix. 您可能已经注意到,“ img_”是通用前缀。 What follows is a two-digit hexadecimal number. 接下来是两位十六进制数。 After another underscore comes an integer that has to be a power of two, somewhere between 1 through 512. Yet another underscore is next. 下一个下划线之后是一个整数,该整数必须是2的幂,介于1到512之间。下一个是下一个下划线。

Okay so this far, my regular expression is working flawlessly. 到目前为止,我的正则表达式工作正常。 The rest is what I'm having problems with: Because what can follow is either a pair of integer coordinates (can be 0), separated by an underscore, or an x. 剩下的就是我遇到的问题:因为可以跟随的是一对下划线分隔的整数坐标(可以为0)或x。 After this comes the final ".png". 之后是最终的“ .png”。 Done. 做完了

Now the main problem I am having is that both variants have to be possible, and also it is highly important that there may not be any duplicate coordinates. 现在,我面临的主要问题是这两种变体都必须可行,而且非常重要的一点是,不得有任何重复的坐标。 Most importantly, integers, both positive and negative, may never start with one or more zeros ! 最重要的是,正负的整数都不能以一个或多个零开头 This would produce duplications like: 这将产生类似的重复:

401 = 00401
-10 = -0010

This is my first attempt: 这是我的第一次尝试:

img_[0-9a-fA-F]{2}_(1|2|4|8|16|32|64|128|256|512)_([-]?[1-9])?[0-9]*_([-]?[1-9])?[0-9]*[.]png

Thanks for your help in advance, 谢谢您的帮助

Tom S. 汤姆·S。

Why use regular expressions? 为什么要使用正则表达式? Why not create a class that decomposes either variant of String to a canonical String, give the class a hashCode() and equals() method that uses this canonical String and then create a HashSet of these objects to make sure that only one of these types of files exist? 为什么不创建一个将String的任一变体分解为规范的String的类,为该类提供使用该规范的String的hashCode()equals()方法,然后创建这些对象的HashSet,以确保仅这些类型之一的文件存在吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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