正则表达式 匹配单词(多词) 并替换

fmxzhou 发布于 2011/07/26 15:26
阅读 3K+
收藏 0

假设有一段英文

According to an investigative report by the government

需要把 an 替换成 <a> an </a>。

            investigation  替换成<a>investigation</a>

 

但是如果是多词的话 就整个替换:

例如  an investigation 之间替换成 <a> an investigation</a>

请问下这个正则替换怎么写??

谢谢

以下是问题补充:

@fmxzhou: $str ='According to an investigative report by the government'; $arr = array('an','an investigative','government');//不固定 foreach($str as $row) { $arr = preg_replace('待写模式', '待替换字符', $str); } 结果应该是 'According to <a>an investigative</a> report by the <a>government</a>'; (2011/07/26 17:46)
加载中
1
云空未必空
云空未必空
        String aa = "According to an dd investigative report by the government";
        System.out.println(aa.replaceAll("(an investigative|an|investigative)", "<a>$1</a>"));

fmxzhou
fmxzhou
谢谢你,但是和我的需求不同!
0
fmxzhou
fmxzhou

个人补充下需求说明吧!!

		$str  ='According to an investigative report by the government';
		$arr  = array('an','an investigative','government');//不固定
		foreach($str as $row)
		{
			$arr = preg_replace('待写模式', '待替换字符', $str);
		}
		
		结果应该是
		'According to <a>an investigative</a> report by the <a>government</a>';
谢谢!

0
William
William
沒明白, 什麼情況單個? 什麼情況一起替換?
0
fmxzhou
fmxzhou

引用来自“William”的答案

沒明白, 什麼情況單個? 什麼情況一起替換?
比如说数组里面有 'hello'  'hello world' 这两个单词,就替换hello world 就可以了
返回顶部
顶部