我要採取一切誰位於正則表達式一個新的線串之間的串正則表達式得到新線
string someStr = "first
second
third
"
例如:
string str1 = "first";
string str2 = "second";
string str3 = "third";
我要採取一切誰位於正則表達式一個新的線串之間的串正則表達式得到新線
string someStr = "first
second
third
"
例如:
string str1 = "first";
string str2 = "second";
string str3 = "third";
只是爲「\ n」把它分解;
someStr.split("\n")
而且可以過濾空字符串,如果你想
或者,如果你真的想正則表達式,做/^.*$/
具有多行標誌
List<String> listOfLines = new ArrayList<String>();
Pattern pattern = Pattern.compile("^.*$", Pattern.MULTILINE);
Matcher matcher = pattern.matcher("first\nsecond\nthird\n");
while (matcher.find()) {
listOfLines.add(matcher.group());
}
然後你;
listOfLines.get(0) = first
listOfLines.get(1) = second
listOfLines.get(2) = third
@Howto你嘗試這個? –
第一個 第二個 第三個這樣的數據只是例如不是無處不在 –
@你可以舉一個例子,打破它嗎? –
或者,如果你只是想每一行的第一個字;
^(\w+).*$
帶有多行標誌。 Regex101有一個很好的正則表達式測試工具:https://regex101.com/r/JF3cKR/1
查看正則表達式標籤描述,有一個風味/上下文缺失。 – LotPings
感謝所有,但我的意思是: '字符串someStr = 「第一 第二 第三 」' 僞代碼 'STR1 = someStr.RegExpr()// STR1只包含第一line' –