說明
^([0-9]+[.](?=[.]+)|[0-9]+[.][0-9]+|[.][0-9]+)|(?<=[0-9])[.]|[.](?=[0-9]|$)
替換:$1
這個正則表達式將執行以下操作:
或周圍的數字發現
例
現場演示
https://regex101.com/r/pF4aS3/2
示例文本小數點
84.34356
.3948
but will match.:
86..3232
84.54.23.
更換後
84.34356
.3948
but will match.:
86.3232
84.5423
說明
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
[.] any character of: '.'
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
[.]+ any character of: '.' (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
[.] any character of: '.'
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[.] any character of: '.'
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
(?<= look behind to see if there is:
----------------------------------------------------------------------
[0-9] any character of: '0' to '9'
----------------------------------------------------------------------
) end of look-behind
----------------------------------------------------------------------
[.] any character of: '.'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[.] any character of: '.'
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
[0-9] any character of: '0' to '9'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
$ before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
所以'86..3232'是一個有效的輸入? – anubhava
我想匹配第二個'。'所以它可以很容易地被刪除。 – Alex
然後只匹配['^(\ d +(?:\。\ d +)?)$'](https://regex101.com/r/iC3xV5/1) – Tushar