简体   繁体   中英

How to match all characters between `<` and `>` except `/`?

I want to extract all characters between < and > except / . So if it is </root> it should extract root and ignore / .

This is code for extracting for values between < and > :

/<(.*?)>/

How do I modify it to ignore / but still get the other characters?

You are close. Just add an optional slash after the opening pointy brace:

</?(.*)>

You should also probably not just accept any characters, but exclude the closing pointy brace:

</?([^>]*)>

The [^...] is a negated group that says "anything that's not in the group. The [ and ] mark the group, and the ^ as the first character negates.

That said, it's a really bad idea to parse XML with regular expressions. You should probably look into a XML parser.

If you are not parsing XML and want to exclude the slashes anywhere in the captured text (ie, not just right after the < ), you could do a Java text replacement like result.replace("/", ""); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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