Remove decimal point when not between two digits

前端 未结 3 1282
太阳男子
太阳男子 2021-01-05 02:55

I\'m cleaning up a search string and need to remove any periods that appear but keep decimal points when they are between two digits.

For example if I have a strin

相关标签:
3条回答
  • 2021-01-05 03:32

    You could use something like \.(?!\d)

    0 讨论(0)
  • 2021-01-05 03:44

    The way I read the question, you want to match a dot only if it's not both preceded and followed by digits. For example, in the following list you would want to match the dot in every string except the last one, because that's the only one that has digits on both sides of it.

    abc.
    .def
    x.y
    123.
    .456
    x.78
    90.x
    599.75

    The accepted answer, (?<!\d)\.(?!\d), matches only in the first three strings; it's equivalent to:

    a dot, ( (not preceded by a digit) AND (not followed by a digit) )
    

    If my interpretation is correct, you want something like this:

    (?<!\d)\.|\.(?!\d)
    

    ...which is equivalent to:

    (a dot, not preceded by a digit) OR (a dot, not followed by a digit)
    

    In any case, it pays to be as precise as you can when talking about text matching, especially when it involves the use of lookarounds.

    0 讨论(0)
  • 2021-01-05 03:47

    You should take advantage of the lookahead and lookbehind assertions. They don't actually match characters in your input, but only determine whether a match is possible or not.
    You can use negative lookaheads and negative lookbehinds to do the opposite of this, which is what's appropriate here. Using the following for strRegEx will match periods that are not surrounded by digits:

    (?<!\d)\.(?!\d)
    
    0 讨论(0)
提交回复
热议问题