使用Java检测标记(单词)

1.字符串分隔

package worddetection;

import java.util.StringTokenizer;

public class WordDetection {
	public static void main(String[] args){
		String input = "\"Let's get this vis-a-vis\", he said, \"these boys' marks are really that well?\"";
		WordDetection wordDetection = new WordDetection();
		wordDetection.useTokenizer(input);
	}
	
	public void useTokenizer(String input){
		System.out.println("Tokenizer");
		StringTokenizer tokenizer = new StringTokenizer(input);
		String word ="";
		while(tokenizer.hasMoreTokens()){
		    word = tokenizer.nextToken();
		    System.out.println(word);
		}
	}
	
}

输出效果:

Tokenizer
"Let's
get
this
vis-a-vis",
he
said,
"these
boys'
marks
are
really
that
well?"

2.分词(BreakIterator)

package worddetection;

import java.text.BreakIterator;

public class WordDetection {
	public static void main(String[] args){
		String input = "\"Let's get this vis-a-vis\", he said, \"these boys' marks are really that well?\"";
		WordDetection wordDetection = new WordDetection();
		wordDetection.useBreakIterator(input);
		
	}
	
	public void useBreakIterator(String input){
		System.out.println("Break Iterator");
		BreakIterator tokenizer = BreakIterator.getWordInstance();
        tokenizer.setText(input);
        int start = tokenizer.first();
        for (int end = tokenizer.next();
             end != BreakIterator.DONE;
             start = end, end = tokenizer.next()) {
             System.out.println(input.substring(start,end));
        }
	}

}

输出效果:

Break Iterator
"
Let's
 
get
 
this
 
vis-a-vis
"
,
 
he
 
said
,
 
"
these
 
boys
'
 
marks
 
are
 
really
 
that
 
well
?
"

3.正则表达式

package worddetection;

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

public class WordDetection {
	public static void main(String[] args){
		String input = "\"Let's get this vis-a-vis\", he said, \"these boys' marks are really that well?\"";
		WordDetection wordDetection = new WordDetection();
		wordDetection.useRegEx(input);
		
	}

	public void useRegEx(String input){
		System.out.println("Regular Expression");
		Pattern pattern = Pattern.compile("\\w[\\w-]+('\\w*)?");
		Matcher matcher = pattern.matcher(input);

		while ( matcher.find() ) {
		    System.out.println(input.substring(matcher.start(), matcher.end()));
		}
	}
}

输出效果:

Regular Expression
Let's
get
this
vis-a-vis
he
said
these
boys'
marks
are
really
that
well

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值