2

I'm trying to compare following strings with regex:

@[xyz="1","2"'"4"] ------- valid
@[xyz] ------------- valid
@[xyz="a5","4r"'"8dsa"] -- valid

@[xyz="asd"] -- invalid
@[xyz"asd"] --- invalid
@[xyz="8s"'"4"] - invalid

The valid pattern should be:

  • @[xyz then = sign then some chars then , then some chars then ' then some chars and finally ]. This means if there is characters after xyz then they must be in format ="XXX","XXX"'"XXX".
  • Or only @[xyz]. No character after xyz.

I have tried following regex, but it did not worked:

String regex = "@[xyz=\"[a-zA-z][0-9]\",\"[a-zA-z][0-9]\"'\"[a-zA-z][0-9]\"]";

Here the quotations (in part after xyz) are optional and number of characters between quotes are also not fixed and there could also be some characters before and after this pattern like asdadad @[xyz] adadad.

4 Answers 4

2

You can use the regex:

@\[xyz(?:="[a-zA-z0-9]+","[a-zA-z0-9]+"'"[a-zA-z0-9]+")?\]

See it

Expressed as Java string it'll be:

String regex = "@\\[xyz=\"[a-zA-z0-9]+\",\"[a-zA-z0-9]+\"'\"[a-zA-z0-9]+\"\\]";

What was wrong with your regex?

  • [...] defines a character class. When you want to match literal [ and ] you need to escape it by preceding with a \.

  • [a-zA-z][0-9] match a single letter followed by a single digit. But you want one or more alphanumeric characters. So you need [a-zA-Z0-9]+

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

Comments

2

Use this:

String regex = "@\\[xyz(=\"[a-zA-z0-9]+\",\"[a-zA-z0-9]+\"'\"[a-zA-z0-9]+\")?\\]";

When you write [a-zA-z][0-9] it expects a letter character and a digit after it. And you also have to escape first and last square braces because square braces have special meaning in regexes.
Explanation:
[a-zA-z0-9]+ means alphanumeric character (but not an underline) one or more times.
(=\"[a-zA-z0-9]+\",\"[a-zA-z0-9]+\"'\"[a-zA-z0-9]+\")? means that expression in parentheses can be one time or not at all.

Comments

2

Since square brackets have a special meaning in regex, you used it by yourself, they define character classes, you need to escape them if you want to match them literally.

String regex = "@\\[xyz=\"[a-zA-z][0-9]\",\"[a-zA-z][0-9]\"'\"[a-zA-z][0-9]\"\\]";

The next problem is with '"[a-zA-z][0-9]' you define "first a letter, second a digit", you need to join those classes and add a quantifier:

String regex = "@\\[xyz=\"[a-zA-z0-9]+\",\"[a-zA-z0-9]+\"'\"[a-zA-z0-9]+\"\\]";

See it here on Regexr

Comments

0

there could also be some characters before and after this pattern like asdadad @[xyz] adadad.

Regex should be:

String regex = "(.)*@\\[xyz(=\"[a-zA-z0-9]+\",\"[a-zA-z0-9]+\"'\"[a-zA-z0-9]+\")?\\](.)*";

The First and last (.)* will allow any string before the pattern as you have mentioned in your edit. As said by @ademiban this (=\"[a-zA-z0-9]+\",\"[a-zA-z0-9]+\"'\"[a-zA-z0-9]+\")? will come one time or not at all. Other mistakes are also very well explained by Others +1 to all other.

Comments

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.