嗨如何从下面的字符串中返回Grapes
,我想搜索一个字符串并在四个字符后返回字符串中间的文本,并丢弃文本的其余部分。搜索一个字符串并返回特定文本android
String grapes = "2 x Grapes @Walmart";
嗨如何从下面的字符串中返回Grapes
,我想搜索一个字符串并在四个字符后返回字符串中间的文本,并丢弃文本的其余部分。搜索一个字符串并返回特定文本android
String grapes = "2 x Grapes @Walmart";
感谢您帮助我的家伙下面的代码工作
String grapes = "2 x Grapes @Walmart";
String[] split = grapes.split("\\s+");
String fsplit = split[2];
干得好。不要忘记将自己的答案标记为已接受以解决您的问题。 – Robin
我的建议是不会为此使用正则表达式。但是为了以防万一,你发现没有倒过来,用这个:
(\w+\s){3}
,你会在第一时间拿到反向引用的第三个单词。 \1
或$1
取其支持你的编译器
这可能会帮助您:
^[\\d]+\\sx\\s(.*?)\\s+.*?$
说明:
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match a single digit 0..9 «[\d]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s»
Match the character “x” literally «x»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s»
Match the regular expression below and capture its match into backreference number 1 «(.*?)»
Match any single character that is not a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match any single character that is not a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
谨慎解释倒票? –
你想'葡萄',总是在4个字符后? ,可能会出现这种情况,当你购买更多的葡萄,并且字符串变成10 X葡萄,即5个字符后 – aelor
所以你想...什么?第三个字?中间的词?前4个字符后的第一个单词? '2x Grapes foo'应该返回什么? – Robin
感谢4或5个字符后丢弃剩余的文字 –