简体   繁体   English

Java RegEx - 特殊字符直到字符串结尾

[英]Java RegEx - special character til end of string

Given rules for Strings are 鉴于字符串的规则是

[Random Nr of Letters]##[Random Nr of Letters]#[Random Nr of Letters]##########

String Length has to be 35 字符串长度必须为35

My String eg looks like My String例如看起来像

TEST##TESTING#TEST#################

My problem is that I can't detect wrong formats like 我的问题是我无法检测到错误的格式

TEST##TESTING#TEST##A##############

My method is: 我的方法是:

private static boolean test(String test_line) {
    String test = "[A-Z]{1,}##[A-Z]{1,}#[A-Z]{1,}#{1,}";
    Pattern test_pattern = Pattern.compile(test);
    Matcher matcher = test_pattern.matcher(test_line);
    return matcher.find();
}

Is there an easy method with RegEx (I have to use it) like "end this String with # and don't allow other characters". 是否有一个简单的方法与RegEx(我必须使用它),如“用#结束此字符串,不允许其他字符”。

Another problem: How can I assure that my test won't allow other characters than AZ 0-9 and #? 另一个问题:我怎样才能确保我的测试不会允许其他字符而不是AZ 0-9和#? Something like: 就像是:

    String test = "([^A-Z][^0-9][#])";
    Pattern test_pattern = Pattern.compile(test);
    Matcher matcher = test_pattern.matcher(test_line);
    return matcher.find();

(with negotiating) (与谈判)

Thanks for the help :) 谢谢您的帮助 :)

Lets do it step by step: 让我们一步一步做:

To ensure that some string has specific length regex can look like 为了确保某些字符串具有特定长度的正则表达式可以看起来像

^.{35}$

where 哪里

  • ^ represents start of string ^表示字符串的开头
  • . represents any character (beside line separators) 代表任何字符(在行分隔符旁边)
  • {35} represents how many times previous element (in out case . - any character) can appear, so here regex require 35 characters {35}代表了多少次前一个元素(以出情况. -任何字符)可以出现,所以这里正则表达式需要35个字符
  • $ represents end of string $表示字符串的结尾

In your case you want to only accept characters in range AZ and # so you can replace . 在您的情况下,您只想接受范围AZ#字符,以便您可以替换. (any character) with this character-class [AZ#] so your regex can look like (任何角色)有这个角色类 [AZ#]所以你的正则表达式看起来像

^[A-Z#]{35}$

But you also want to ensure order of these characters. 但是你也想确保这些角色的顺序。 In other words you also want to check if entire string matches some other regex 换句话说,您还想检查整个字符串是否与其他正则表达式匹配

^[A-Z]{1,}##[A-Z]{1,}#[A-Z]{1,}#{1,}$

actually instead of {1,} we could simply use + which will give us 实际上我们可以简单地使用+来代替{1,}

^[A-Z]+##[A-Z]+#[A-Z]+#+$

To combine these two regexes you can use look-ahead mechanism which let us peek at characters after position where it was used and check if characters after it matches some additional regex. 要结合这两个正则表达式可以使用前瞻机制,让我们窥视字符位置之后,它被使用,它匹配一些额外的正则表达式后检查字符。

So final regex can look like 所以最终的正则表达式看起来像

^(?=[A-Z#]{35}$)[A-Z]+##[A-Z]+#[A-Z]+#+$
    ----------- ------------------------
regex1 (length)  regex2 (order)

Now to avoid recompiling ( Pattern.compile(test) ) the same regex each time your method is called it is better to store its compiled version outside of method as class field. 现在为了避免每次调用方法时重新编译( Pattern.compile(test) )相同的正则表达式,最好将其编译后的版本作为类字段存储在方法之外。 So try maybe something like 所以尝试类似的东西

private static Pattern test_pattern = Pattern
        .compile("^(?=[A-Z#]{35}$)[A-Z]+##[A-Z]+#[A-Z]+#+$");

private static boolean test(String test_line) {
    return test_pattern.matcher(test_line).matches();
}

Test 测试

System.out.println(test("TEST##TESTING#TEST#################"));//true
System.out.println(test("TEST##TESTING#TEST##A##############"));//false

Try this out 试试吧

[a-zA-Z]+#{2}[a-zA-Z]+#{1}[a-zA-Z]+#{1,}

Or 要么

^([a-zA-Z]+#{2}[a-zA-Z]+#{1}[a-zA-Z]+#{1,})$

Or 要么

^(?=.{35}$)([A-Z]+#{2}[A-Z]+#{1}[A-Z]+#{1,})$

TEST##TESTING#TEST################# // pass
TEST##TESTING#TEST##A############## // fail

Demo 演示

The below regex would match the lines which satisfies the above mentioned criteria, 以下正则表达式将匹配满足上述标准的行,

^(?=.{35}$)[A-Z0-9]+##[A-Z0-9]+#[A-Z0-9]+#+$

DEMO DEMO

It matches the line which contains exactly 35 characters. 它匹配包含正好35个字符的行。

For the format specified ensuring the last character is a # : 对于指定的格式,确保最后一个字符是#

([A-Z]+[#]+){3}

To test the length of the string: 要测试字符串的长度:

if(test.length == 35)
    // ...

To test and allow digits as well (only alphanumeric and # sign): 要测试和允许数字(仅限字母数字和#符号):

([A-Z0-9]+[#]+){3}

To allow lower case letters as well: 允许使用小写字母:

(\w+#+){3}

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

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