tags:

views:

97

answers:

4

I must to match the stars(*), that satisfied such conditions:

\*\S.*\S\* -> (for example *y text1 text2 u*)

The problem is, that i want to match only this stars, if the condition is true, how can it be done(desirable in java:))(i have write so:

Pattern params = Pattern.compile("\\*\\S.*\\S\\*");

but this matches stars and text between stars...)

A: 

You must use a non-greedy pattern, like:

Pattern.compile("\\*\\S.*?\\S\\*");

Note that additional ? character

Rubens Farias
A: 

It's not clear why you'd want to do such a thing. If you want to "only match"--do you mean "capture"--the stars, then simply test the given pattern against your target string, and, if it matches, then you know there are two stars around some text, and it doesn't matter whether the regex captured them. It's two stars!

What are you actually trying to do?

Jonathan Feinberg
A: 

I agree with other responders: it is unclear exactly what you are trying to do but if you want anything between two *'s in the text, then:

Pattern params = Patterns.compile ( "\\*[^\\*]+\\*" );

That will find every non-zero length string between two *'s.

With more information we can provide better answers.

PSpeed
Note: the escaping in the brackets is technically unnecessary but I find it confuses fewer people when needing to put a * as an actual character.
PSpeed
+1  A: 

You can use non-capturing groups, like this:

`Pattern pattern = Pattern.compile("(\\*)(?:\\S.*\\S)(\\*)");`

Basically, the match pattern contains 3 groups: (\\*), (?:\\S.*\\S), (\\*). The second group is like (?:re), it is non-capturing group, means let regex don't count this group in the result.

Here is the sample code:

import java.util.regex.Pattern; import java.util.regex.Matcher;

public class RegexTest {

/**
 * @param args
 */
public static void main(String[] args) {


        Pattern pattern = Pattern.compile("(\\*)(?:\\S.*\\S)(\\*)");

        String string ="for example *y text1 text2 u*";
        Matcher matcher = pattern.matcher(string);

        boolean found = false;
        if (matcher.find()) {
            System.out.println("group count:"+matcher.groupCount());
            System.out.println("---------------");
            for(int i=1; i<=matcher.groupCount(); i++)
            {
             System.out.println("group "+i);
             System.out.println("start index:"+matcher.start(i));
             System.out.println("end index:"+matcher.end(i));
             System.out.println("text:"+string.substring(matcher.start(i), matcher.end(i)));
             System.out.println("---------------");
            }

            found = true;
        }
        if(!found){
            System.out.println("not found.");
        }



}

}

Please note in java the group count in Matcher class is 1 based. The group count in the above code is 2.

If you don't use non-capturing group like this:

Pattern pattern = Pattern.compile("(\\*)(\\S.*\\S)(\\*)");

The result will return 3 groups.

For details, please refer this link: http://java.sun.com/docs/books/tutorial/essential/regex/groups.html

Steve Zhang