简体   繁体   English

对Java字符串形成正则表达式

[英]Forming a regular expression to a Java string

I have a String, where only numbers and none, one or more percentages are allowed 我有一个字符串,其中只有数字,没有数字,一个或多个百分比是允许的

so my regex would be: [\\d+%] , you can test it here 所以我的正则表达式为: [\\d+%] ,您可以在这里进行测试

for java i have to transform it, 对于Java,我必须对其进行转换,

public static final String regex = "[\\d+\\%]";

and to test it i use this function 并对其进行测试,我使用此功能

public static final String regex = "[\\d+\\%]";

public boolean validate(String myString){

    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(myString);

    if (!matcher.matches()) {
        return false;
    }else{
        return true;
    }
}

The regular expression is not working, also if i use 正则表达式不起作用,如果我使用

public static final String regex = "[\\d+%]";

Is there any good online tool for escaping a long regular expression for java? 有没有什么好的在线工具可以转义Java的长正则表达式?

A more advanced question: 一个更高级的问题:
the % should be only allowed if a minimum of one digit is in the String, only a % shouldn't be allowed! 只有在字符串中至少包含一位数字时,才应允许%,而不应该仅允许%! And: numbers without a % are only allowed if the number of digits is exactly 8, not less (means: 1234567 is bad, but 12345678 is good) 和:仅当数字的位数恰好为8,而不是更少时,才允许不带%的数字(表示:1234567不好,但12345678很好)

Testcases: 测试用例:

  • Bad: % , 错误: % (empty string), 23b , -1 , 7.5 , %5a , 1 , 1234567 (空字符串), 23b-17.5%5a11234567
  • Good: 12345678 , 23% , 1%53%53 , %7 好: 12345678 23%1%53%53%7

I have a String, where only numbers and none, one or more percentages are allowed so my regex would be: [\\d+%] 我有一个字符串,其中只能有数字,没有数字,一个或多个百分比是允许的,所以我的正则表达式为: [\\d+%]

Actually, that matches ONE character which may be a digit, a + or a % . 实际上,它匹配一个字符,该字符可以是数字, +%

To match what you have described in words, you need something like this: 为了匹配您用语言描述的内容,您需要以下内容:

  [\d%]*\d[\d%]*

which matches a string containing at least one digit with optional percent signs. 与至少包含一位数字和可选百分号的字符串匹配。 Note that the % character is not a meta-character and hence doesn't need to be escaped in the regex. 请注意, %字符不是元字符,因此不需要在正则表达式中转义。 It will match all of the following: 它将符合以下所有条件:

  0
  00
  %0
  0%0
  00%
  0%0%0
  0%%0

and so on, but not just % or any string that contains characters other than digits or % characterss. 依此类推,但不仅限于%或任何包含数字或%字符以外的字符的字符串。


Is there any good online tool for escaping a long regular expression for java? 有没有什么好的在线工具可以转义Java的长正则表达式?

I'm not aware of one. 我不知道一个。 But escaping wasn't the reason your regex wasn't working. 但是转义并不是您的正则表达式无法正常工作的原因。


A more advanced question: the % should be only allowed if a minimum of one digit is in the String, only a % shouldn't be allowed! 一个更高级的问题:仅当字符串中至少有一位数字时才应允许%,而不应仅允许%!

I think my regex above does that. 我认为我上面的正则表达式可以做到这一点。 And for the record, here is what it looks like as a Java String literal: 作为记录,这是Java String文字的样子:

"[\\d%]*\\d[\\d%]*"

Unless you have TAB, NL, CR, etc characters in the regex , it is sufficient to just replace each individual \\ with \\\\ . 除非您在正则表达式中包含TAB,NL,CR等字符,否则只需将每个\\替换为\\\\就足够了。

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

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