嗨,我正在使用 Regex 有条件地省略 java 中的文本。
String pattern= "(#<omit_[a-zA-Z0-9_]+>)(.+?)(<omit_[a-zA-Z0-9_]+>)";
Mary had a #<omit_size>little <omit_size>lamb, his fleece was white as snow.
在上面的示例中,我的模式可以很好地找到需要隐藏(或不隐藏)的内容。
当我可能需要将某些东西隐藏在可能也需要隐藏的东西中时,我的问题就出现了。例如:
Mary had a #<omit_segment> #<omit_size>little <omit_size>lamb, his <omit_segment> fleece was white as snow.
上述模式的局限性在于它无法区分可能遵循的许多字符排列'#<omit_'
。因此,如果我只想隐藏位于'#<omit_segment>... <omit_segment>'
My pattern 之间的内容,而是查看 '#<omit_segment> ...#<omit_size> '
and,因此将打印:
Mary had a little <omit_size>lamb, his <omit_segment> fleece was white as snow.
而不是想要的:
Mary had a fleece was white as snow.
我意识到 Regex 可能不是解决此类问题的答案,但在我撤消很多东西之前,我想我会看看是否有什么可以挽救的。所以..有什么想法吗?
谢谢。