Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

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:

String 1: With serial

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

String 2: Without Serial

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

As we can see the incoming string can have the /456/1111 or it may not have. How can I write a single regex function which checks whether it is present or not? I have written a regex but it would work only if the /456/1111 is present:

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. I feel like I am missing some small thing.

question from:https://stackoverflow.com/questions/66065583/java-string-match-using-the-regex-for-optional-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 
 (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 
, and the end of the
                           string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...