欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java在字符串中查找需要的字段 博客分类: SorLib  

程序员文章站 2024-03-12 08:12:38
...
该方法可以将一段字符串中特定的字符取出:
例如:aaaaa[T]0909,[e],wer32[a],wrwerwe[asdfst],
程序最终取出的字段是:Tea

String test = getTextFromClipboard();
		if (null == test) {
			return;
		}
		test = test.replaceAll("\\[", "%").replaceAll("\\]", "%");
		Pattern p = Pattern.compile("%[A-Za-z]{0,2}+[0-9]{0,2}%");
		Matcher matcher = p.matcher(test);

		String word = null;
		String[] keys = new String[3];
		int count = 0;
		while (matcher.find()) {
			word = matcher.group();
			keys[count++] = word.replaceAll("%", "");
			if (3 == count) {
				break;
			}
		}

		if (3 != count) {
			return;
		}