【发布时间】:2015-11-02 22:33:59
【问题描述】:
假设我有类似的东西:
Sample 1: Your number is <foo>12345</foo> and your code is <foo>29939</foo>.
Sample 2: Your number is <foo attr="x">12345</foo> and your code is <foo>29939</foo>.
我想把这个字符串分解成一个字符串数组。
示例 1 如下所示:
array[0] = Your number is
array[1] = 12345
array[2] = and your code is
array[3] = 29939
示例 2:
array[0] = Your number is
array[1] = x|12345 (adding attr value to it)
array[2] = and your code is
array[3] = 29939
我正在寻找 <foo> 在字符串中有或没有属性,需要相应地打破字符串。
我找到了一种简单的方法来用一些值替换下面的东西。
示例:matcher.replaceAll("bar") 导致类似:
Your number is bar and your code is bar
每当我在字符串值中看到标记<foo> 时,我希望看到将字符串分解为数组或列表。
【问题讨论】:
-
String.split 是你所需要的
-
可以嵌套标签吗?喜欢
abc <foo>def<bar>123</bar>xyz</foo>?如果是,应该如何处理? -
@JunedAhsan 我不这么认为。 OP 需要更像解析这半个 xml 字符串的内容。
-
最好使用 xml 解析器。如果您不需要在标签中获取属性值形式,那么您可以轻松完成 string.split("
| ") -
没有经过测试的标签...我只是在寻找
和 可能有一个属性的东西,我们称之为“attr”,而且 不区分大小写,因为我在常规字符串中间获取 XML...我找到了使其不区分大小写等的方法,
标签: java arrays regex string split