假設我有一個由數字、字母和符號組成的字串,
String foo = "987abc<*(123";
我如何才能在 String中的最后一組數字 ( "1", at index 6) 中找到第一個數字的索引foo- 如果foo = "123",我將再次獲得第一個數字的索引 ( "1", at index 0)。(并且還讓它回傳索引0if foo = "")
我對 RegEx-es 和類似的搜索模式非常缺乏經驗,而且我嘗試過的每一種方式都沒有正常作業(大多數情況下會導致StringIndexOutOfBoundsException)。
在 String 中找到最后一組數字的開頭索引的可靠、簡單的方法是什么?
uj5u.com熱心網友回復:
嘗試這個。
static int indexOfLastNumber(String s) {
int removedLength = s.replaceFirst("\\d \\D*$", "").length();
return s.length() == removedLength ? 0 : removedLength;
}
static void test(String s) {
System.out.println(s " : " indexOfLastNumber(s));
}
public static void main(String[] args) {
test("987abc<*(123");
test("987abc<*(123)");
test("123");
test("foo");
test("");
}
輸出:
987abc<*(123 : 9
987abc<*(123) : 9
123 : 0
foo : 0
: 0
或者
static final Pattern LAST_NUMBER = Pattern.compile("\\d \\D*$");
static int indexOfLastNumber(String s) {
Matcher m = LAST_NUMBER.matcher(s);
return m.find() ? m.start() : 0;
}
uj5u.com熱心網友回復:
您可以使用 Regex 命名組獲取它
public static int indexOfLastNumber(String text) {
Pattern pattern = Pattern.compile("(\\d )(?!.*\\d)");
Matcher matcher = pattern.matcher(text);
return matcher.find() ? matcher.start() : -1;
}
我使用了@csalmhof 回答中的測驗用例,感謝他
public static void main(String[] args) {
System.out.println("\"987abc<*123\"" " index of lastNumberSet: " indexOfLastNumber("987abc<*123"));
System.out.println("\"987abc<*123abc\"" " index of lastNumberSet: " indexOfLastNumber("987abc<*123abc"));
System.out.println("\"987abc\"" " index of lastNumberSet: " indexOfLastNumber("987abc"));
System.out.println("\"abc987\"" " index of lastNumberSet: " indexOfLastNumber("abc987"));
System.out.println("\"987\"" " index of lastNumberSet: " indexOfLastNumber("987"));
System.out.println("(Empty String)" " index of lastNumberSet: " indexOfLastNumber(""));
System.out.println("\"abc\"" " index of lastNumberSet: " indexOfLastNumber("abc"));
}
輸出,-1 表示沒有數字的文本
"987abc<*123" index of lastNumberSet: 8
"987abc<*123abc" index of lastNumberSet: 8
"987abc" index of lastNumberSet: 0
"abc987" index of lastNumberSet: 3
"987" index of lastNumberSet: 0
(Empty String) index of lastNumberSet: -1
"abc" index of lastNumberSet: -1
uj5u.com熱心網友回復:
注意:“1”在您的字串中的索引 9 處。
如果您不想,則無需為此使用 RegEx。
這樣的方法應該可以完成這項作業:
public static int findLastNumbersIndex(String s) {
boolean numberFound = false;
boolean charBeforeNumberFound = false;
//start at the end of the String
int index = s.length() - 1;
//loop from the back to the front while there are more chars
//and no nonDigit is found before a digit
while (index >= 0 && !charBeforeNumberFound) {
//when the first number was found, set the boolean flag
if (!numberFound && Character.isDigit(s.charAt(index))) {
numberFound = true;
}
//when already a number was found and there is any nonDigit stop the execution
if (numberFound && !Character.isDigit(s.charAt(index))) {
charBeforeNumberFound = true;
break;
}
index--;
}
return index 1;
}
不同字串的執行:
public static void main(String[] args) {
System.out.println("\"987abc<*(123\"" " index of lastNumberSet: " findLastNumbersIndex("987abc<*(123"));
System.out.println("\"987abc<*(123abc\"" " index of lastNumberSet: " findLastNumbersIndex("987abc<*(123abc"));
System.out.println("\"987abc\"" " index of lastNumberSet: " findLastNumbersIndex("987abc"));
System.out.println("\"abc987\"" " index of lastNumberSet: " findLastNumbersIndex("abc987"));
System.out.println("\"987\"" " index of lastNumberSet: " findLastNumbersIndex("987"));
System.out.println("(Empty String)" " index of lastNumberSet: " findLastNumbersIndex(""));
System.out.println("\"abc\"" " index of lastNumberSet: " findLastNumbersIndex("abc"));
}
回傳此輸出:
"987abc<*(123" index of lastNumberSet: 9
"987abc<*(123abc" index of lastNumberSet: 9
"987abc" index of lastNumberSet: 0
"abc987" index of lastNumberSet: 3
"987" index of lastNumberSet: 0
(Empty String) index of lastNumberSet: 0
"abc" index of lastNumberSet: 0
uj5u.com熱心網友回復:
您可以使用帶有捕獲組的模式,如果有匹配項,您可以使用public int start(int group)來獲取匹配器捕獲組的起始索引。
(\d)\d*\D*$
(\d)捕獲組 1 中的一位數\d*匹配可選數字\D*匹配可選的非數字$字串結束
查看正則運算式演示和Java 演示
例子:
String[] strings = { "987abc<*(123", "", "123", "test", "abc123" };
Pattern pattern = Pattern.compile("(\\d)\\d*\\D*$");
for (String s : strings) {
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
System.out.printf("'%s' --> %d\n", s, matcher.start(1));
continue;
}
System.out.printf("'%s' --> %d\n", s, 0);
}
輸出
'987abc<*(123' --> 9
'' --> 0
'123' --> 0
'test' --> 0
'abc123' --> 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/369583.html
