嗨如何從下面的字符串中返回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個字符後丟棄剩餘的文字 –