微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正则表达式对JX8NET很有用 给定一段文章 查找hi这个单词

地址:正则表达式对JX8NET很有用 给定一段文章 查找hi这个单词

正则表达式对JX8NET很有用 给定一段文章,查找hi这个单词。


\bhi\b

3.我要找出这样的一个字符串,包含is,然后,在is后面跟着一个perfect。
\bis\b.*\bperfect\b

package com.pattern.test;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class PatternTest {

public static void main(String[] args) {

String pattern = "\\bis\\b.*\\bperfect\\b";

String testStr = "RegexBuddy is your perfect companion for working with regular expressions";

// pattern = args [0];

// testStr = args [1];

Matcher matcher = Pattern. compile(pattern).matcher(testStr);

if (matcher.find()) {

System. out.println("[" +testStr+"]" + " matches pattern " +"["+ pattern+"]" );

System. out.println("After replace,the new testStr is:[" + matcher.replaceFirst("TARGET" )+"]" );

System. out.println("the origl testStr is:[" + testStr+"]");

} else {

System. out.println("[" +testStr+"]" + " does not match pattern " + "["+ pattern+ "]");

}

}

}

-----
输出

[RegexBuddy is your perfect companion for working with regular expressions] matches pattern [\bis\b.*\bperfect\b]

After replace,the new testStr is:[RegexBuddy TARGET companion for working with regular expressions]

the origl testStr is:[RegexBuddy is your perfect companion for working with regular expressions]

原文地址:https://www.jb51.cc/regex/361605.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐