0

I have to use java regex to match string like this: [] [a1] ,[abdf123] ...:

that is ,a brackets with a string inside it, this inside string is empty ,or it has to be like this: aaa123, a12, b34, that is , a sequence of letters, and then a sequence of digits.

So I write the regex like this:

Pattern.matches("^(\\[)[|[a-zA-Z]+(\\d+)](\\])$","[abc123]");

But to my surprise ,it returns false. To test if [a-zA-Z]+(\d+) can correctly match the string inside the [],I write the regex like this:

Pattern.matches("^[a-zA-Z]+(\\d+)$","abc123");

it returns true;

Anybody can explain this for me?thanks.

1
  • @Jerry Sorry ,I have already edited my question.My fault! Commented Dec 30, 2013 at 8:58

3 Answers 3

1

It looks like the second left bracket (the one that's not escaped) is starting a character set. And maybe you need parentheses around the empty case.

Removing the brackets matches

Pattern.matches("^(\\[)[a-zA-Z]+(\\d+)(\\])$", "[abc123]")

And to include the empty case, try something like

^(\\[\\])|(\\[[a-zA-Z]+\d+\\])$

I put the empty check in a different place (to make it more explicit), but it's basically the same regular expression absent the extra set of brackets.

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

2 Comments

What do you mean by I need parentheses around the empty case?Can you show me ?
I'm not sure if you absolutely did need them in the OP. Probably that comment just confused the issue. Sorry about that.
1

The issue is that you're using a big character class [ ... ] (this means that + is not working as intended) to put the parts you want to match. Instead, remove the outer square brackets and the parentheses, they are not needed, maybe except to capture the text (I'll put that later on).

Pattern.matches("^\\([[a-zA-Z]+\\d+)?\\]$","[abc123]");

That should return true.


Now to capture the text:

Pattern pattern = Pattern.compile("^\\[([a-zA-Z]+\\d+)?\\]$");
Matcher matcher = pattern.matcher("[abc123]");
while (matcher.find()) {
    System.out.println(matcher.group(1));
}

ideone demo.

2 Comments

,would you please explain to me my fault?In what place am I wrong?
@Vico_Wu Like I said, you used [ ... ] in your regex which means a character class. This causes [|[a-zA-Z]+(\\d+)] to match either one of those: | or [a-zA-Z] or + or ( or \\d or + or ). You need to use parentheses instead. For instance, (|[a-zA-Z]+\\d+) would work just as well.
1

I think you made an incomplete attempt to OR the two patterns [] and [<optional>]. Your pattern starts with ^(\\[)[|[...;; the first pattern in your OR opens two brackets but never closes them. Depending on the grouping that you want to achieve the OR is more elaborate.

If you want to match [] and [abc123], but not [abc] and [123] then you can group the pattern inside the brackets, characters and digits, and match that group 0 or once.

Pattern.matches("^(\\[)([a-zA-Z]+(\\d+))?(\\])$","[abc123]");

It matches the following groups for [abc123] as input:

0: [0,8] [abc123]
1: [0,1] [
2: [1,7] abc123
3: [4,7] 123
4: [7,8] ]

6 Comments

Sorry , I have edited my question.But "^(\[)[a-zA-Z]+(\\d+)(\])$" cannot match "[]"?
Ah, sorry. For that all you need to do is replace your quantifier to match 0 or more occurances. I have updated my answer.
No no no."^(\[)[a-zA-Z]*(\\d*)(\])$" will also match [123] or [abc].They are all not permitted.
I see. Updated the answer again.
Thank you @Ralf.Would you please tell me in what place am I wrong?
|

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.