简体   繁体   English

查找XML标签值的正确正则表达式-Java

[英]Find correct regex for XML tag value - java

I would like to find correct regex to get value of xml tag and replace it with X . 我想找到正确的正则表达式来获取xml标签的值并将其替换为X This tag: 这个标签:

<number>1234I0000ABC0001</number>

I creted regex like this: 我像这样创建正则表达式:

.*number>([A-Z0-9 _]*[A-Z0-9][A-Z0-9 _]*)</

but it is not work as weel as I want. 但它不像我想要的那样工作。 I would like to get the value by regex, replace all characters with X and set this changed value into tag. 我想通过正则表达式获取值,将所有字符替换为X ,并将此更改后的值设置为标记。

It s not a good idea to parse XML with regex. 用正则表达式解析XML不是一个好主意。 But if you insist then you can use 但是,如果您坚持,那么您可以使用

<number>([\\s\\S]*?)<\\/number>

this will capture the value as Group 1 . 这将捕获该值作为Group 1 You can easily replace that with whatever you like. 您可以轻松地用自己喜欢的任何东西替换它。 For detail explanation you can visit this regex101 in live action 有关详细说明,您可以现场访问此regex101

You might look at something like this : 您可能会看到以下内容

(<.+>)(.+)(</.+>)

or 要么

<number>(.+?)</number>

I have to note that it is not actually a number :-) 我必须指出,它实际上不是数字:-)

It'll be group(1) 分组(1)

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

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