srinivas m
srinivas m

Reputation: 19

Regular Expression for particular format

Need one Regular expression for finding the below format in Java

ABC.CDB.ASD[0].QWE[0]

Tried this: \\b\\w{3}+\\.\\w{3}+\\.\\w{3}\\[.+?\\]+\\.\\w\\[.+?\\]+\\b

but failed.

Without boundaries it is working if I give the specific length for the last array like:

  \\w{3}+\\.\\w{3}+\\.\\w{3}\\[.+?\\]+\\.\\w{3}\\[.+?\\]+

Want boundaries and last word without length.

Can any body help please?

Upvotes: 0

Views: 68

Answers (5)

Joe Saad
Joe Saad

Reputation: 1950

I tried this one and it works. You can remove the small letters a-z if you want to restrict it to only capital letters.

([a-zA-Z]{3}.){2}[a-zA-Z]+\[\d+\].[a-zA-Z]+\[\d+\]$

Upvotes: 1

Toto
Toto

Reputation: 91395

Just add a + after the last \\w:

\\b\\w{3}+\\.\\w{3}+\\.\\w{3}\\[.+?\\]+\\.\\w+\\[.+?\\]+
//                                    here __^

IFAIK, there're no needs of \\b at the end.

Upvotes: 0

Shekhar Khairnar
Shekhar Khairnar

Reputation: 2691

try this may help:

Pattern pattern = Pattern.compile("\\w{3}\\.\\w{3}\\.\\w{3}\\[\\d{1}\\]\\.\\w{3}\\[\\d{1}\\]");

Upvotes: 0

Bohemian
Bohemian

Reputation: 425023

The problem is the trailing \b. \b means a "word boundary" - between a word and a non-word. Unless your target is followed by a word char, you won't get a match. ie the input would have to look like:

ABC.CDB.ASD[0].QWE[0]A

Also the trailing + after the ] seem unnecessary. Try this:

\\w{3}+\\.\\w{3}+\\.\\w{3}\\[.+?\\]\\.\\w\\[.+?\\]

Upvotes: 0

cigno5.5
cigno5.5

Reputation: 712

I think you have too many "+" symbol. The right regexp should be the following: \w{3}\.\w{3}\.\w{3}\[\d+\]\.\w{3}\[\d+\]

Why have you put "+" after "\w{3}"?

Upvotes: 1

Related Questions