Balconsky
Balconsky

Reputation: 2244

Pattern in java language

I try to get from String like (111,222,ttt,qwerty) list of values

I try this pattern:

String area = "(111,222,ttt,qwerty)";
    String pattern = "\\([([^,]),*]+\\)";
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(area);
            System.out.println(m.groupCount());
            ArrayList<String> values = new ArrayList<String>();
            while(m.find()){
                System.out.println("group="+m.group(1));
                values.add(m.group());
            }

But I found that group count is zero. What I`ve missed?

Upvotes: 1

Views: 125

Answers (4)

Peter Lawrey
Peter Lawrey

Reputation: 533890

If you know there is only one set of brackets ()

String text = "aaa,bbb(111,222,ttt,qwerty),,,cc,,dd";
String[] parts = text.substring(text.indexOf('(')+1, text.indexOf(')')).split(",");
// parts = [ 111, 222, ttt, qwerty ]

Upvotes: 0

Manjula
Manjula

Reputation: 5101

If you only have words containing english letters and numbers without spaces,

You can use following regexp for achieving this.

String pattern = "[a-zA-Z0-9]+";

It checks for groups of characters which only contains digits and uppercase/lowercase English letters.

Upvotes: 0

Tudor
Tudor

Reputation: 62469

Assuming you always have the same format of the string, you could simply try:

String[] split = area.split("\\(|\\)|,");

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109613

It should be (...)+ not [...]+ (character).

Upvotes: 0

Related Questions