简体   繁体   English

使用正则表达式在Java中进行模式匹配

[英]pattern matching in java using regular expression

I am looking for a pattern to match this "LA5@10.232.140.133@Po6" and one more "LA5@10.232.140.133@Port-channel7" expression in Java using regular expression. 我正在寻找一种使用正则表达式匹配"LA5@10.232.140.133@Po6"和另一个"LA5@10.232.140.133@Port-channel7"表达式的模式。

Like we have \\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3} for IP address validation. 就像我们有\\ d {1,3}。\\ d {1,3}。\\ d {1,3}。\\ d {1,3}一样,用于IP地址验证。

Can we have the pattern like below? 我们可以有下面的模式吗? Please suggest-- 请提出-

[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]@\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}@Po\d[1-9]
[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]@\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}@Port-channel\d[1-9]

Thanks in advance. 提前致谢。

============================== =============================

In my program i have, 在我的程序中,

import java.util.regex.*;
class ptternmatch {
    public static void main(String [] args) {
        Pattern p = Pattern.compile("\\w\\w\\w@\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}@*");
        Matcher m = p.matcher("LA5@10.232.140.133@Port-channel7");
        boolean b = false;
        System.out.println("Pattern is " + m.pattern());
        while(b = m.find()) {
            System.out.println(m.start() + " " + m.group());
    }
    }
}

But i am getting compilation error with the pattern.--> Invalid escape sequence The sequence will be like a ->a 3 character word of digit n letter@ipaddress@some text.. 但是我在使用该模式时遇到编译错误。->无效的转义序列该序列将类似于-> 3个字符的数字n字母@ ipaddress @一些文本的单词。

Well, if you want to validate the IP address, then you need something a little bit more involved than \\d{1,3} . 好吧,如果您想验证IP地址,那么您需要比\\d{1,3}多一些的东西。 Also, keep in mind that for Java string literals, you need to escape the \\ with \\\\ so you end up with a single backslash in the actual regex to escape a character such as a period ( . ). 另外,请记住,对于Java字符串文字,您需要使用\\\\转义\\ ,因此最终在实际的正则表达式中使用单个反斜杠来转义诸如句点( . )的字符。

Assuming the LA5@ bit is static and that you're fine with either Po or Port-channel followed by a digit on the end, then you probably need a regex along these lines: 假设LA5@位是静态的,并且您可以使用PoPort-channel ,然后在末尾添加一个数字,那么您可能需要以下正则表达式:

LA5@(((2((5[0-5])|([0-4][0-9])))|(1[0-9]{2})|([1-9][0-9]?)\\.){3}(2(5[0-5]|[0-4][0-9]))|(1[0-9]{2})|([1-9][0-9]?)@Po(rt-channel)?[1-9]

(Bracketing may be wonky, my apologies) (对不起,请稍等,抱歉)

You can do something like matcher.find() and, if it is true, the groups to capture the information. 您可以执行matcher.find()类的matcher.find() ,如果为true,则可以执行组以捕获信息。 Take a look a the tutorial here: 在这里看看教程:

You would need to wrap the necessary parts int parentheses - eg (\\d{1,3}) . 您需要将必要的部分包装在int括号中-例如(\\d{1,3}) If you wrap all 4, you will have 4 groups to access. 如果将所有4个都打包,则将有4个组可以访问。

Also, take a look at this tutorial 另外,请看一下本教程

It's a very good tutorial, I think this one would explain most of your questions. 这是一个很好的教程,我认为这将解释您的大多数问题。

To match the second of your strings: 匹配第二个字符串:

  • LA5@10.232.140.133@Port-channel7 LA5 @ 10.232.140.133 @ Port-channel7

you can use something like: 您可以使用类似:

\w{2}\d@\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}@[a-zA-Z\-]+\d

This depends on what you want to do, so the regex might change. 这取决于您要执行的操作,因此正则表达式可能会更改。

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

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