简体   繁体   English

Java 字符串匹配使用可选字符串的正则表达式

[英]Java String match using the regex for optional String

I have the following Java String which I need to compare with the regex but these string can consist of the optional values which may be present or not present I need to perform different things based on their availability:我有以下 Java 字符串,我需要与正则表达式进行比较,但这些字符串可以包含可能存在或不存在的可选值我需要根据它们的可用性执行不同的事情:

String 1: With serial字符串 1:带序列号

uri =  https://myid.com/123/1234567890128/456/1111

String 2: Without Serial字符串 2:无序列号

uri = https://myid.com/123/1234567890128

As we can see the incoming string can have the /456/1111 or it may not have.正如我们所看到的,传入的字符串可以有/456/1111 ,也可能没有。 How can I write a single regex function which checks whether it is present or not?如何编写单个正则表达式 function 来检查它是否存在? I have written a regex but it would work only if the /456/1111 is present:我写了一个正则表达式,但只有/456/1111存在时它才会起作用:

uri.matches("(http|https)://.*/123/[0-9]{13}.*)")

I tried adding the optional values after looking at some of the answers here something like this:在查看此处的一些答案后,我尝试添加可选值,如下所示:

uri.matches("(http|https)://.*/123/[0-9]{13}+([/456/[0-9]{1,20}]?)")

But for some reason, it does not work.但由于某种原因,它不起作用。 Can someone please help me how can I verify whether there are any strings present in uri /456/1111 or not.有人可以帮助我如何验证 uri /456/1111中是否存在任何字符串。 I feel like I am missing some small thing.我觉得我错过了一些小东西。

regex101.com is your friend in this regard. regex101.com在这方面是你的朋友。

When looking at your regex on that site , you can see some errors like:在该站点上查看您的正则表达式时,您会看到一些错误,例如:

  • you have a lone ] at the end which seems off你有一个孤独的]在最后似乎关闭
  • and at last your ?最后是你的? at the end targets the wrong group, move it out of the parenthesis.最后针对错误的组,将其移出括号。

Something like https?://[^/]+/123/\d{13}(?:/456/\d{1,20})?https?://[^/]+/123/\d{13}(?:/456/\d{1,20})? should work for you.应该为你工作。

The good thing about regex101 is that on the right side you see a detailed explanation about your regex, and it highlights exactly which character does what. regex101 的好处在于,在右侧您可以看到有关您的正则表达式的详细说明,并且它准确地突出显示了哪个字符做了什么。

Use利用

^https?:\/\/.*\/123\/[0-9]{1,13}(?:/456/[0-9]{1,20})?$

See proof .证明

Explanation解释

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  http                     'http'
--------------------------------------------------------------------------------
  s?                       's' (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  :                        ':'
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  123                      '123'
--------------------------------------------------------------------------------
  \/                       '/'
--------------------------------------------------------------------------------
  [0-9]{1,13}              any character of: '0' to '9' (between 1
                           and 13 times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    /456/                    '/456/'
--------------------------------------------------------------------------------
    [0-9]{1,20}              any character of: '0' to '9' (between 1
                             and 20 times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

The reason the pattern that you tried does not work, is because in the last part of your pattern you have ([/456/[0-9]{1,20}]?) which means:您尝试的模式不起作用的原因是因为在您的模式的最后一部分中您有([/456/[0-9]{1,20}]?)这意味着:

  • ( Capture group (捕获组
    • [/456/[0-9]{1,20} Match 1-20 repetitions of either / or a digit 0-9 (as 0-9 also matches 456) [/456/[0-9]{1,20}匹配/或数字 0-9 的 1-20 次重复(因为 0-9 也匹配 456)
    • ]? Match optional ]匹配可选]
  • ) Close group )关闭组

What you could do instead, is making the last group as a whole optional without a character class use https?相反,您可以做的是使最后一组作为一个整体可选,没有字符 class 使用https? making the s optional.使 s 可选。

^https?://.*/123/[0-9]{13}(?:/456/[0-9]{1,20})?$

Regex demo |正则表达式演示| Java demo Java演示

As you use matches() it should match the whole string and you can omit the anchors ^ and $当你使用matches()它应该匹配整个字符串,你可以省略锚^$

String uri1 =  "https://myid.com/123/1234567890128/456/1111";
String uri2 = "https://myid.com/123/1234567890128";
String uri3 = "https://myid.com/123/1234567890128/456/111122222222222222222";
String pattern = "https?://.*/123/[0-9]{13}(?:/456/[0-9]{1,20})?";

System.out.println(uri1.matches(pattern));
System.out.println(uri2.matches(pattern));
System.out.println(uri3.matches(pattern));

Output Output

true
true
false

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

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