Remove text between the tags using regular express

2019-02-24 11:24发布

I have following xml tags: <EmployerName>company name</EmployerName> and <Email>name@gmail.com</Email>

I am using the following regular expression- <EmployerName>[0-9A-Z:-]*</EmployerName> to remove the data between tags. But, the data is not getting removed. Any idea?

3条回答
Emotional °昔
2楼-- · 2019-02-24 11:52
<EmployerName>[^<]*</EmployerName>

The working DEMO

查看更多
聊天终结者
3楼-- · 2019-02-24 12:03

If you want to match everything within the tags, just use .:

<EmployerName>.*</EmployerName>

Your character group [0-9A-Z:-] covers digits, letters, the colon and the hyphen characters, but it doesn't include whitespace or other special characters.

Then you can replace with simply <EmployerName></EmployerName>.

In case there are multiple EmployerName elements on the same line, use a reluctant match:

<EmployerName>.*?</EmployerName>
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-02-24 12:09

Your expression is missing the space “company name”. Try replacing the following:

(<\/?(?:EmployerName|Email)>)[a-z \.@]+(<\/?(?:EmployerName|Email)>)

… with $1$2. Backreferencing didn’t work in Notepad++, so it’s a little verbose.

查看更多
登录 后发表回答