简体   繁体   English

删除正则表达式/正则表达式所使用的数字范围

[英]Remove a rang of numbers by which regular expression / regex

I have a XMLexport tool (PHP) which exports data to an XML field. 我有一个XMLexport工具(PHP),可将数据导出到XML字段。

It is possible to find and replace some unnecessary characters. 可以找到并替换一些不必要的字符。 But in this case the characters are not always the same. 但是在这种情况下,字符并不总是相同。

Eg <xmlfield> {"1596":" maat XL </xmlfield> --> this must be <xmlfield> maat XL </xmlfield> 例如<xmlfield> {"1596":" maat XL </xmlfield> --> this must be <xmlfield> maat XL </xmlfield>

So I want to remove {"1596":" . Only the numeric code changes, eg {"1595":" {"844":" . 所以我想删除{"1596":" 。仅数字代码更改,例如{"1595":" {"844":"

The solution is "find and replace" with a regular expression, but I don't know which code I have to use. 解决方案是使用正则表达式“查找并替换”,但是我不知道必须使用哪个代码。

Please let me known if you can help me with this. 如果您可以帮助我,请告诉我。

I use this site when I need to work out regular expressions: RegExr 当我需要计算正则表达式时,我使用此站点: RegExr

this code will replace what you want though: 该代码将替换您想要的内容:

Regex reg = new Regex(@"\{\""\d+\"":\""");
reg.Replace(xmlString, string.Empty);

The regular expression to do that would be: \\{"\\d+"\\:" Depending on the tool or programming language you are using you might have to escape the " as well. 要执行此操作的正则表达式为: \\{"\\d+"\\:"根据所使用的工具或编程语言,您可能还必须转义" Java also needs to escape the \\ with another \\ . Java也需要逃避\\与其他\\

If you are not familiar with regex, let me explain it shortly: 如果您不熟悉正则表达式,请让我简短地说明一下:

Some characters like { or : need to be escaped (therefore we add a \\ before that). 像某些字符{或者:需要进行转义(因此我们增加了一个\\之前)。 \\d matches any digit (0-9) and the + means it has to be at least one digit (but possibly more). \\d匹配任何数字(0-9),而+表示它必须至少为一位(但可能更多)。

You can use online tools like RegExPlanet to create and test your regex. 您可以使用RegExPlanet等在线工具来创建和测试您的正则表达式。

If you want to find those strings only inside <xmlfield> tags (without other nested tags in it) you could use something like this: <xmlfield>[^<]*\\{"\\d+"\\:"[^<]*</xmlfield> 如果您只想在<xmlfield>标记内找到这些字符串(不包含其他嵌套的标记),则可以使用以下内容: <xmlfield>[^<]*\\{"\\d+"\\:"[^<]*</xmlfield>

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

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