Vizible’s Blog

ทดสอบ Java Regex

Posted in Programming by wearetherock on December 22, 2008

จุดประสงค์ : อยากได้ ประโยคที่มี  *

package rss.zdrop;

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

/**
 *
 * @author beer
 */
public class TestWildcard {

    public static void main(String[] args) {
        String reg = "[^\\&|\\|\\(]*[\\w]*\\*[^\\&\\|\\)]*'*";
        Pattern pattern = Pattern.compile(reg);
        
        String req = "requirements = (Os_Name == 'Windows*' || Os_Version == '10.5.*.*'
&& Comp_Name == 'ubuntu05');";
        Matcher matcher = pattern.matcher(req);

        boolean found = false;
        Vector<String> wildcard = new Vector<String>();
        
        while (matcher.find()) {
            System.out.println(String.format("I found the text \"%s\" starting at " +
                    "index %d and ending at index %d.%n",
                    matcher.group(), matcher.start(), matcher.end()));
            found = true;
            
            wildcard.add(matcher.group().trim());
        }
        
        if (!found) {
            System.out.println(String.format("No match found.%n"));
        }
    
        System.out.println(wildcard);
    }
}

Output :

I found the text “Os_Name == ‘Windows*’ ” starting at index 16 and ending at index 38.

I found the text ” Os_Version == ‘10.5.*.*’  ” starting at index 40 and ending at index 67.

[Os_Name == ‘Windows*’, Os_Version == ‘10.5.*.*’]

Advertisements
Tagged with: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: