简体   繁体   English

Java ReplaceAll在这里没有帮助我

[英]Java ReplaceAll doesn't help me here

I have a String here, 我这里有一个琴弦

String message = "hi test message >< abcd < pqr <variable> xyz <tag> testing <>";

I want to replace all <> <tag> <anything> with (.*) as i am trying to build a regex out of it. 我想用(.*)替换所有<> <tag> <anything> ,因为我试图从中构建一个正则表达式。

I tried the following, 我尝试了以下方法

message = message.replaceAll("<(.*)>", "(.*)");

but the result is 但结果是

hi test message >(.*)

the result i am expecting is this 我期望的结果是

hi test message >< abcd < pqr (.*) xyz (.*) testing (.*)

Can anyone help me to acheive this result? 谁能帮助我达到这个结果?

ThankYou in advance! 先感谢您!

To catch tag contents, try this regexp (assuming they're never nested): 要捕获标签内容,请尝试以下正则表达式(假设它们从未嵌套):

<([^<>])*>

Note that this will completely ignore this part of your test string: 请注意,这将完全忽略测试字符串的这一部分:

< abcd < pqr

使用以下正则表达式删除所有类型的标签。

@"(?></?\w+)(?>(?:[^>'""]+|'[^']*'|""[^""]*"")*)|(?>(?:[>'""]+|'[^']*'|""[^""]*"")*)"

Provided you really mean to only replace <> , <tag> , <anything> and nothing else, try: 如果您真的要只替换<><tag><anything>而别无其他,请尝试:

<(tag|anything)?>

If you want to match all enclosed tags, including empty ones: 如果要匹配所有封闭的标签,包括空标签:

<[^<>]*>

您可以使用此正则表达式

"<(|tag|anything)>"

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

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