首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除自定义标记标记所需的正则表达式

删除自定义标记标记所需的正则表达式
EN

Stack Overflow用户
提问于 2012-04-03 22:44:45
回答 3查看 280关注 0票数 2

我以前没有写过正则表达式,我的知识非常不足。我希望这里的专家能够帮助我使用一个正则表达式,我可以在C#中使用它来删除标记标记。

该标记具有以下开始标记之一:<AI>!<AH>!<AG>!,并以另一个!结束

示例:the quick brown <AI>!fox jumps! over the lazy dog!

删除标记后应为:the quick brown fox jumps over the lazy dog!

代码片段:

代码语言:javascript
复制
    NOT MORE THAN 85 % OF H<AH>!3!BO<AH>!3! CALCULATED ON THE DRY WEIGHT
- Uranium ores and pitchblende, and concentrates thereof, with a uranium content of more than 5 % by weight (<AI>!Euratom!)
- Monazite; urano-thorianite and other thorium ores and concentrates, with a thorium content of more than 20 % by weight (<AI>!Euratom!)
- - - -   -94% or more, but not more than 98.5% of a-Al<AH>!2!O<AH>!3!  -2% (+/-1.5%) of magnesium spinel,  -1% (+/-0.6%) of yttrium oxide and   -2% (+/-1.2%) of each lanthanum oxide and neodymium oxide  with less than 50% of the total weight having a particle size of more than 10mm
- Activated alumina with a specific surface area of at least 350 m<AG>!2!g
IRON OXIDES AND HYDROXIDES; EARTH COLOURS CONTAINING 70 % OR MORE BY WEIGHT OF COMBINED IRON EVALUATED AS FE<AH>!2!O<AH>!3!:
- <AI>!o!-Xylene
- <AI>!m!-Xylene
- <AI>!p!-Xylene
- - - 1,6,7,8,9,14,15,16,17,18,18-Dodecachloropentcyclo[12.2.1.1<AG>!6,9!.0<AG>!2,13!.0<AG>!5,10!]octadeca-7,15-diene, (CAS RN 13560-89-9)
- Chlorobenzene, <AI>!o!-dichlorobenzene and <AI>!p!-dichlorobenzene
- - - Di- or tetrachlorotricyclo[8.2.2.2<AG>!4,7!]xadeca-1(12),4,6,10,13,15-hexaene, mixed isomers
- Butan-1-ol (<AI>!n!-butyl alcohol)
- - 2-Methylpropan-2-ol (<AI>!tert!-butyl alcohol)
- <AI>!n!-Butyl acetate
- <AI>!O!-Acetylsalicylic acid, its salts and esters
- - <AI>!O!-Acetylsalicylic acid (CAS RN 50-78-2)
- 1-Naphthylamine (<AH>!alpha!-naphthylamine), 2-naphthylamine (<AI>!beta!-naphthylamine) and their derivatives; salts thereof
- <AI>!o!-, <AG>!m!-, <AH>!p!-Phenylenediamine, diaminotoluenes, and their derivatives; salts thereof:
- - <AI>!o!-, <AI>!m!-, <AI>!p!-Phenylenediamine, diaminotoluenes and their halogenated, sulphonated, nitrated and nitrosated derivatives; salts thereof:
- - Indole, 3-methylindole (skatole), 6-allyl-6,7-dihydro-5<AI>!H!-dibenz[<AI>!c,e!] azepinne (azapetine), phenindamine (INN) and their salts; imipramine hydrochloride (INNM)
- Vitamin B<AH>!1! and its derivatives
- Vitamin B<AH>!2! and its derivatives

提前谢谢你

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-04-03 22:51:37

要使用的正则表达式将查找A,后跟<>!中包含的GHI之一。找到后,它将对一个或多个(+)任何内容(.)执行惰性搜索(由表示),并后跟一个感叹号。它是懒惰的,所以在找到样本中的最后一个感叹号之前,它不会进行查找,而是会停在第一个感叹号处,并替换它找到的内容。然后它将使用grouping (模式中的括号)来存储标签中包含的值,并在替换时使用它($1表示第一个组)。

代码语言:javascript
复制
var r = new Regex("<A[GHI]>!(.+?)!");
var actual = r.Replace(xml, "$1");
票数 5
EN

Stack Overflow用户

发布于 2012-04-03 22:53:02

要使用的正则表达式必须如下所示:

代码语言:javascript
复制
\<..\>!([^!]*)!

因为您必须匹配<两个字母>!一系列不带a!的字符最后是一个!再来一次。

然后用捕获的匹配项(即括号之间的文本)替换匹配项(匹配上面表达式的整个文本)。

票数 0
EN

Stack Overflow用户

发布于 2012-04-03 22:56:28

代码语言:javascript
复制
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
       string pattern =  @"\<A(G|H|I)\>\!([^\!]*)\!";
       string input = "<AI>!n!-Butyl acetate the quick brown "
           + "<AI>!fox jumps! over the lazy dog!";
       string replacement = "$2";
       Regex rgx = new Regex(pattern);
       string result = rgx.Replace(input, replacement);

       Console.WriteLine("Original String:    '{0}'", input);
       Console.WriteLine("Replacement String: '{0}'", result);                             
    }
}

Original String:    '<AI>!n!-Butyl acetate the quick brown <AI>!fox jumps! over the lazy dog!'
Replacement String: 'n-Butyl acetate the quick brown fox jumps over the lazy dog!'

http://ideone.com/z0fbL

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9995661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档