简体   繁体   English

简化复杂的长正则表达式

[英]Simplifying a long complex regex

I suck at creating regex each time i have to check an input.每次我必须检查输入时,我都无法创建正则表达式。

I have to check that inputs have the correct format.我必须检查输入的格式是否正确。 Input format could be :输入格式可以是:

  1. AA:BB:CC DDD/EEE
  2. CC DDD/EEE

Don't mind the uppercase.不要介意大写。 A,B,C and D can be either a letter (in uppercase or not) or a digit. A、B、C 和 D 可以是字母(大写或非大写)或数字。

I came up with this regex (which works) but how it can be simplified or even optimized.我想出了这个正则表达式(有效),但如何简化甚至优化它。

([a-zA-Z0-9])*([:])?([a-zA-Z0-9])*([:])?([a-zA-Z0-9])+([ ]){1}([a-zA-Z0-9])+([/]){1}([a-zA-Z0-9])+
  • If you only put one character in the [] s, then the [] s are redundant, so [/] can be simplified to / , [:] can be simplified to : etc.如果只在[]放一个字符,那么[]是多余的,因此[/]可以简化为/[:]可以简化为:等。

  • You also don't need to specify that something repeats {1} time, so those can be removed.您也不需要指定某些内容重复{1}次,因此可以删除这些内容。

  • 0-9 inside the [] s can be simplified to \\d : []0-9可以简化为\\d

Applying the above, we get:应用以上,我们得到:

([a-zA-Z\d])*(:)?([a-zA-Z\d])*(:)?([a-zA-Z\d])+( )([a-zA-Z\d])+(/)([a-zA-Z\d])+
  • (:)? will either capture : or nothing ( null ).将捕获:或什么都不捕获( null )。 If you don't need this, you can remove the group.如果您不需要这个,您可以删除该组。 Similarly ( ) will always capture a space, which seems quite pointless.同样, ( )总是会捕获一个空格,这似乎毫无意义。

  • ([a-zA-Z\\d])* will only capture the last repeat. ([a-zA-Z\\d])*只会捕获最后一次重复。 You might want ([a-zA-Z\\d]*) , or not to capture anything.您可能想要([a-zA-Z\\d]*) ,或者不捕获任何内容。

Assuming you don't want to capture anything, hence removing all the groups, we get:假设您不想捕获任何内容,因此删除所有组,我们得到:

[a-zA-Z\d]*:?[a-zA-Z\d]*:?[a-zA-Z\d]+ [a-zA-Z\d]+/[a-zA-Z\d]+

Last but not least:最后但并非最不重要的:

  • At the start, [a-zA-Z\\d]*:?一开始, [a-zA-Z\\d]*:? is repeated twice, we can use a {2} quantifier.重复两次,我们可以使用{2}量词。

  • If you pass the Pattern.CASE_INSENSITIVE option to Pattern.compile , you don't need to specify AZ every time.如果将Pattern.CASE_INSENSITIVE选项传递给Pattern.compile ,则不需要每次都指定AZ

Now we get:现在我们得到:

([a-z\d]*:?){2}[a-z\d]+ [a-z\d]+/[a-z\d]+

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

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