简体   繁体   English

匹配 JAVA 中特殊字符之间的字符串

[英]matching a string between special characters in JAVA

I want to match n number of characters between a "<" and ">" characters.我想在“<”和“>”字符之间匹配 n 个字符。 For example, I want to be able to match <a href = "image1.jpg"> or <a href = " http://www.learnmore.com/&gt ; essentially with the same pattern. This is because, the strings I receive might have n number of characters between the special characters. Once I match the pattern which includes the special characters, I will replace it with a blank space(which I am able to do currently---the replacing part.). I need help with the matching part only.例如,我希望能够匹配<a href = "image1.jpg"><a href = " http://www.learnmore.com/&gt ;基本上具有相同的模式。这是因为,字符串我收到的特殊字符之间可能有 n 个字符。一旦匹配包含特殊字符的模式,我将用空格替换它(我目前能够做到这一点---替换部分。)。我只需要匹配部分的帮助。

You can try the following regular expression你可以试试下面的正则表达式

(&lt;|<)(.*?)(&gt;|>)

to match against your string (assuming the &gt; was not a formatting error in your question but the actual content in the string).与您的字符串匹配(假设&gt;不是您问题中的格式错误,而是字符串中的实际内容)。

First replace &gt;首先替换&gt; with > and &lt;>&lt; with < .< Then do your pattern matching as usual using regex or whatever.然后像往常一样使用正则表达式或其他方式进行模式匹配。

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

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