简体   繁体   English

仅使用RegEx替换段落中单词的首次出现

[英]Replace ONLY first occurrence of word from a paragraph using RegEx

I have two paragraphs. 我有两段。 I want to replace ONLY first occurrence of a specific word ' acetaminophen ' by ' {yootooltip :: It is a widely used over-the-counter analgesic (pain reliever) and antipyretic (fever reducer). 我只想将特定单词“ 对乙酰氨基酚 ”的首次出现替换为“ {yootooltip ::这是一种广泛使用的非处方镇痛药(止痛药)和退烧药(退烧药)”。 Excessive use of paracetamol can damage multiple organs, especially the liver and kidney.}acetaminophen{/yootooltip} ' 过量使用对乙酰氨基酚会损害多个器官,尤其是肝脏和肾脏。}对乙酰氨基酚{/ yootooltip} '

The paragraph is: Percocet is a painkiller which is partly made from oxycodone and partly made from acetaminophen . 该段为: Percocet是一种止痛药,部分由羟考酮制成,部分由对乙酰氨基酚制成。 It will usually be prescribed for a patient who is suffering from acute severe pain. 通常会给患有严重剧烈疼痛的患者开处方。 Because it has oxycodone in it, this substance can create an addiction and is also a dangerous prescription drug to abuse. 因为它含有羟考酮,所以这种物质会引起成瘾,并且也是滥用的危险处方药。 It is illegal to either sell or use Percocet that has not been prescribed by a licensed professional. 出售或使用未经许可的专业人士未规定的Percocet是违法的。 In 2008, drugs like Percocet (which have both oxycodone and acetaminophen as their main ingredients) were the prescription drugs most sold in all of Ontario. 2008年,像Percocet这样的药物(以羟考酮和对乙酰氨基酚为主要成分)是安大略省销售最多的处方药。 The records also show that the rates of death by oxycodone (this includes brand name like Percocet) doubled. 记录还显示,羟考酮(包括诸如Percocet之类的品牌名称)的死亡率也翻了一番。 That is why it is imperative that the people who are addicted go to an Ontario drug rehab center. 这就是为什么成瘾者必须去安大略省戒毒所的原因。 Most of the drug rehabs can take care of Percocet addiction. 大多数戒毒所都可以治疗Percocet上瘾。

I am trying to write a regular expression for this. 我正在为此写一个正则表达式。 I have tried \\bacetaminophen\\b But it is replacing both occurrences. 我已经尝试过\\ bacetaminophen \\ b,但是它替代了两种情况。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

Use the optional $limit parameter in PHP's preg_replace function http://us2.php.net/manual/en/function.preg-replace.php 在PHP的preg_replace函数http://us2.php.net/manual/zh/function.preg-replace.php中使用可选的$limit参数

$text = preg_replace('/acetaminophen/i', 'da-da daa', $text, 1);

will replace only the first occurance. 将仅替换第一次出现。

对于您正在使用的工具 ,只需使用以下命令:

(.*?)\bacetaminophen\b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM