简体   繁体   中英

Complex Java Regular Expression with Nested Groupings

I am trying to get a regular expression written that will capture what I'm trying to match in Java, but can't seem to get it.

This is my latest attempt:

Pattern.compile( "[A-Za-z0-9]+(/[A-Za-z0-9]+)*/?" );

This is what I want to match:

  • hello
  • hello/world
  • hello/big/world
  • hello/big/world/

This what I don't want matched:

  • /
  • /hello
  • hello//world
  • hello/big//world

I'd appreciate any insight into what I am doing wrong :)

试试这个正则表达式:

Pattern.compile( "^[A-Za-z0-9]+(/[A-Za-z0-9]+)*/?$" );

Doesn't your regex require question mark at the end?

I always write unit tests for my regexes so I can fiddle with them until they pass.

// your exact regex:
final Pattern regex = Pattern.compile( "[A-Za-z0-9]+(/[A-Za-z0-9]+)*/?" );

// your exact examples:
final String[]
    good = { "hello", "hello/world", "hello/big/world", "hello/big/world/" },
    bad = { "/", "/hello", "hello//world", "hello/big//world"};

for (String goodOne : good) System.out.println(regex.matcher(goodOne).matches());
for (String badOne : bad) System.out.println(!regex.matcher(badOne).matches());

prints a solid column of true values.

Put another way: your regex is perfectly fine just as it is.

It looks like what you're trying to 'Capture' is being overwritten each quantified itteration. Just change parenthesis arangement.

  #  "[A-Za-z0-9]+((?:/[A-Za-z0-9]+)*)/?"

 [A-Za-z0-9]+ 
 (                                  # (1 start)
      (?: / [A-Za-z0-9]+ )*
 )                                  # (1 end)
 /?

Or, with no capture's at all -

 #  "[A-Za-z0-9]+(?:/[A-Za-z0-9]+)*/?"

 [A-Za-z0-9]+ 
 (?: / [A-Za-z0-9]+ )*
 /?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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