我目前正在尋找匹配(負數)數字的正則表達式,除了包含在日期時間格式中的數字。正則表達式匹配除了包含在dateTime格式中的所有數字
例:
WHERE id=2 and date > 1990-11-10
=> 2
亦宜。 我嘗試了幾個解決方案,但沒有給我除了結果。 這裏是我已經嘗試過:
[-]?\d+[^(\d+-\d+-\d+)]
=>炭火類值超出範圍
(?<=(=|>|<|))[-]?\d+$
=>不壞,但不匹配的數字是不是在該行的末尾
(?<=(=|>|<|))[-]?\d+
=>不排除dateTime格式的第一個數字
(?<=(=|>|<|))[-]?\d+(<!(:|-))
=>無效的外觀模式。
最後的解決方案似乎是最好的,但我不明白這個錯誤。 我用這個鏈接http://www.regular-expressions.info/lookaround.html來構建它們,並用rubular進行測試。
這裏是我的測試表:http://rubular.com/r/2bXr53XTpq
BTW:這裏是我們使用的代碼:
public static String formatCondition(String condition) {
if (condition != null)
{
try
{
Pattern pNumbers = Pattern.compile("(?<=(=|>|<|))[-]?\\d+");
Matcher mNumbers = pNumbers.matcher(condition);
condition = mNumbers.replaceAll("'$1'");
} catch (Exception e)
{
e.printStackTrace();
}
}
return condition;
}
所以我在尋找正確的正則表達式或其他解決方案。 謝謝!
編輯:錯誤「無效的外觀模式」顯然只在Ruby中出現(因爲rubular是Ruby,但仍然比其他的更好)。我測試http://www.regexe.com/
,但仍然錯誤的結果('22'和'00'匹配)
編輯2:VKS的回答:(?<=[^\w-:])[+-]?\d+(?=\s|$)
工程,我讓如果這裏如果別人需要!
噢耶快速的答案和工作完美!謝謝 ! – user3876391
@ user3876391確實接受,如果它爲你工作:) – vks
一分鐘計時器:p – user3876391