解码正则表达式- ^[事情就让它a-zA-Z0-9“,,! # $ %()* +,”/:;? @[\ \]^ _ { | } ~)+美元

时间:2023-01-30 21:16:46

Using Java

使用Java

I am not a regular on regex, I came across the following regex as part of migration of springmodules-validation stuff to latest.

我不是正则表达式的正则表达式,我遇到了下面的regex作为springmodules-validation的迁移的一部分。

^[a-zA-Z0-9 "'&!#$%()*+,-./:;?@[\\]^_`{|}~]+$

What exactly is this doing? I need to understand this to write unit test to this validation. By the way I'm using it in a Java project.

这到底是怎么回事?我需要理解这一点,才能为这种验证编写单元测试。顺便说一下,我在Java项目中使用它。

One more interesting thing, I tried this expression in hibernate-validator as follows:

更有趣的是,我在hibernate-validator中尝试过这个表达式,如下所示:

@Pattern(regexp = "^[a-zA-Z0-9 "'&!#$%()*+,-./:;?@[\\]^_`{|}~]+$")

Then my intellijIDEA shows an error at the end of the line saying Unclosed character class. is the regex expression is properly formed?

然后,我的intellijIDEA在一行末尾显示了一个错误,说的是Unclosed character类。regex表达式是否格式正确?

Update

更新

It seems the expression is malformed, I see the following exception while trying to test this:

似乎这个表达是畸形的,我在试着测试这个的时候看到了以下的例外:

java.util.regex.PatternSyntaxException: Unclosed character class near index 57
^[a-zA-Z0-9 "'&!#$%()*+,-./:;?@[\]^_`{|}~]+$

Here is the original expression from one of the xml files which I'm trying to migrate:

下面是我要迁移的其中一个xml文件的原始表达式:

<regexp apply-if="creativeType == 'Text'" expression="^[a-zA-Z0-9 

&quot;&apos;&amp;!#$%()*+,-./:;?@[\\]^_`{|}~]+$"/>

Am I missing anything?

我错过什么吗?

Working Solution

工作方案

regexp = "^[a-zA-Z0-9 \"'&!#$%()*+,-./:;?@\\[\\]^_`{|}~]+$"

this way I have assigned it to a string and which works perfectly for me Thank you all!

这样我就把它分配给了一个字符串,它对我来说非常有用,谢谢大家!

2 个解决方案

#1


4  

The translated expression would look something like

翻译后的表达式应该是这样的

^[a-zA-Z0-9 "'&!#$%()*+,-./:;?@\[\]^_`{|}~]+$

and means a line of letter, digits and a set of other characters (like different brackets, where ] has to be escaped for not meaning the end of a character class).

并且表示一行字母、数字和一组其他字符(如不同的括号,其中)必须转义,因为不表示字符类的结束)。

#2


0  

You can use something like YAPE::Regex::Explain in Perl or RegexBuddy to get a detailed description of your regular expression. A messy one-liner can be found below:

您可以使用类似YAPE::Regex::Explain的Perl或RegexBuddy来获得正则表达式的详细描述。下面是一个凌乱的一行代码:

perl -MYAPE::Regex::Explain -e \
'$e=<>; print YAPE::Regex::Explain->new($e)->explain';

After providing the regexp from stdin:

提供stdin的regexp后:

The regular expression:

^[a-zA-Z0-9 "'&!#$%()*+,-./:;?@[\]^_`{|}~]+$

matches as follows:

NODE                       EXPLANATION
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  ^                        the beginning of the string
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  [a-zA-Z0-9               any character of: 'a' to 'z', 'A' to 'Z',
  "'&!#$%()*+,-             '0' to '9', ' ', '"', ''', '&', '!', '#',
  ./:;?@[\]^_`{|}~]+       '$', '%', '(', ')', '*', '+', ',' to '.',
                           '/', ':', ';', '?', '@', '[', '\]', '^',
                           '_', '`', '{', '|', '}', '~' (1 or more
                           times (matching the most amount possible))
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  $                        before an optional \n, and the end of the
                           string
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Using something like Regex Buddy will let you select a Java flavor for your regular expression, but it should be pretty standard in this case.

使用Regex Buddy这样的工具可以让您为正则表达式选择Java风格,但在这种情况下,它应该是相当标准的。

Are you sure this is Java though? From all that escaping it looks a lot more like it's part of a XSD / XPath / XML thing.

你确定这是Java吗?从所有的溢出来看,它看起来更像是XSD / XPath / XML的一部分。

#1


4  

The translated expression would look something like

翻译后的表达式应该是这样的

^[a-zA-Z0-9 "'&!#$%()*+,-./:;?@\[\]^_`{|}~]+$

and means a line of letter, digits and a set of other characters (like different brackets, where ] has to be escaped for not meaning the end of a character class).

并且表示一行字母、数字和一组其他字符(如不同的括号,其中)必须转义,因为不表示字符类的结束)。

#2


0  

You can use something like YAPE::Regex::Explain in Perl or RegexBuddy to get a detailed description of your regular expression. A messy one-liner can be found below:

您可以使用类似YAPE::Regex::Explain的Perl或RegexBuddy来获得正则表达式的详细描述。下面是一个凌乱的一行代码:

perl -MYAPE::Regex::Explain -e \
'$e=<>; print YAPE::Regex::Explain->new($e)->explain';

After providing the regexp from stdin:

提供stdin的regexp后:

The regular expression:

^[a-zA-Z0-9 "'&!#$%()*+,-./:;?@[\]^_`{|}~]+$

matches as follows:

NODE                       EXPLANATION
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  ^                        the beginning of the string
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  [a-zA-Z0-9               any character of: 'a' to 'z', 'A' to 'Z',
  "'&!#$%()*+,-             '0' to '9', ' ', '"', ''', '&', '!', '#',
  ./:;?@[\]^_`{|}~]+       '$', '%', '(', ')', '*', '+', ',' to '.',
                           '/', ':', ';', '?', '@', '[', '\]', '^',
                           '_', '`', '{', '|', '}', '~' (1 or more
                           times (matching the most amount possible))
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  $                        before an optional \n, and the end of the
                           string
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Using something like Regex Buddy will let you select a Java flavor for your regular expression, but it should be pretty standard in this case.

使用Regex Buddy这样的工具可以让您为正则表达式选择Java风格,但在这种情况下,它应该是相当标准的。

Are you sure this is Java though? From all that escaping it looks a lot more like it's part of a XSD / XPath / XML thing.

你确定这是Java吗?从所有的溢出来看,它看起来更像是XSD / XPath / XML的一部分。