forum

home / developersection / forums / java pattern for [j-*]

Java pattern for [j-*]

Anonymous User 2533 14-Oct-2013

Please help me  with the pattern matching. I want to build a pattern which will match the word starting withj- or c- in the following in a string (Say for example)

[j-test] is a [c-test]'s name with [foo] and [bar]

The pattern needs to find [j-test] and [c-test] (brackets inclusive).

What I have tried so far?

String template = "[j-test] is a [c-test]'s name with [foo] and [bar]";

Pattern patt = Pattern.compile("\\[[*[j|c]\\-\\w\\-\\+\\d]+\\]");
Matcher m = patt.matcher(template);
while (m.find()) {
    System.out.println(m.group());
}


And its giving output like

[j-test]

[c-test]
[dood]
[bar]

which is wrong. Please help me, thanks for your time on this thread.


Updated on 14-Oct-2013

I am a content writter !


Message
Can you answer this question?

Answer

1 Answers

Liked By