1

Here is the regex:

ws(s)?://([0-9\.a-zA-Z\-_]+):([\d]+)([/([0-9\.a-zA-Z\-_]+)?

Here is a test pattern:

wss://beta5.max.com:18989/abcde.html

softlion.com likes it:

Test results
Match count: 1

Global matches:
wss://beta5.max.com:18989/abcde.html

Value of each capturing group:
0    1   2   3   4
wss://beta5.max.com:18989/abcde.html     s   beta5.max.com   18989   /abcde.html

scala does not:

val regex = """ws(s)?://([0-9\.a-zA-Z\-_]+):([\d]+)([/([0-9\.a-zA-Z\-_]+)?""".r


Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 58
ws(s)?://([0-9\.a-zA-Z\-_]+):([\d]+)([/([0-9\.a-zA-Z\-_]+)?

2 Answers 2

1

My bad, I had an extra [ at the front of the last capturing group.

([/([0-9.a-zA-Z-_]+)?

Sign up to request clarification or add additional context in comments.

Comments

1

Java allows intersections and all that, so error ..

     ws
     ( s )?
     ://
     ( [0-9\.a-zA-Z\-_]+ )
     :
     ( [\d]+ )
=    (  <-- Unbalanced  '('
=         [  <-- Unbalanced  '['
          /
          ( [0-9\.a-zA-Z\-_]+ )?

With everybody else its no problem:

 ws
 ( s )?                             # (1)
 ://
 ( [0-9\.a-zA-Z\-_]+ )              # (2)
 :
 ( [\d]+ )                          # (3)
 ( [/([0-9\.a-zA-Z\-_]+ )?          # (4)

So, its good to see (know) the original regex is not what you thought it was.

3 Comments

I had not heard about intersections. How does the unbalanced ([ relate?
Hi, did you get a chance to describe how the exta ([ relates to intersections? thx
@javadba - Was gone, didn't see this. I've written so many regular expression parsers, it's all a blur. Java (and Python?) won't support inner (inside a class) raw opening bracket without a corresponding closing brace. Intersections are prefaced by && literals, unions aren't (which is irrelevant). With everybody else, you can include the opening bracket and its just a literal. Look at this docs.oracle.com/javase/tutorial/essential/regex/… for the gory details of java classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.