---
title: "Java pattern for [j-*]"  
description: "Java pattern for [j-*]"  
author: "Anonymous User"  
published: 2013-10-14  
updated: 2013-10-14  
canonical: https://www.mindstick.com/forum/1666/java-pattern-for-j  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Java pattern for [j-*]

Please help me with the [pattern matching](https://www.mindstick.com/articles/1865/pattern-matching-in-erlang). I want to build a pattern which will [match](https://www.mindstick.com/forum/159713/regex-match-attribute-in-an-html-code) the [word](https://www.mindstick.com/forum/305/read-word-file) [starting](https://www.mindstick.com/blog/12024/things-you-need-to-know-when-starting-your-own-business) withj- or c- in the following in a [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) ([Say](https://answers.mindstick.com/qa/116645/a1-wreckers-reviews-what-do-customers-say) for example)

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

The pattern needs to find [j-[test](https://yourviews.mindstick.com/story/1427/explosive-facts-about-trinity-test-world-s-first-nuclear-bomb)] 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](https://yourviews.mindstick.com/view/80639/regifting-is-far-better-than-giving-away-a-bouquet) [output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular) like

```
[j-test]
[c-test]
[dood]
[bar]
```

which is wrong. Please help me, thanks for your time on this thread.

## Replies

### Reply by Anonymous User

Inside a character class, you don't need to use alternation to match j or c. Character class itself means, match any single character from the ones inside it. So, [jc] itself will match either j or c.

Also, you don't need to match the [pattern](https://www.mindstick.com/forum/2314/what-is-the-mvp-and-mvc-pattern) that is after j- or c-, as you are not bothered about them, as far as they start with j- or c-.

Simply use this pattern:

```
Pattern patt = Pattern.compile("\\[[jc]-[^\\]]*\\]");
```

To explain:

\

```
Pattern patt = Pattern.compile("(?x)      "   // Embedded flag for Pattern.COMMENT
                             + "\\[       "   // Match starting `[`
                             + "    [jc]  "     // Match j or c
                             + "    -     "     // then a hyphen
                             + "    [^    "     // A negated character class
                             + "       \\]"        // Match any character except ]
                             + "    ]*    "     // 0 or more times
                             + "\\]       "); // till the closing ]
```

Using (?x) flag in the regex, ignores the whitespaces. It is often helpful, to write readable regexes.


---

Original Source: https://www.mindstick.com/forum/1666/java-pattern-for-j

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
