1

Can a regular expression be used to find all magic number integers in Visual Studio and convert them to doubles?

The regular expression to find them is beyond my skill, here is what I came up with so far:

(?!=[\s\(])(?<!\.)\d+(?=[\s\)])

This will find all the integers, but erroneously matches

15.527 = "1"5.5"2""7"

Even with a perfect match, is there a way to replace with same number? For example 7 would be replaced by 7.0 and 16 would be replaced by 16.0

Adam
  • 1,825
  • 1
  • 17
  • 24
  • Try `(?<![0-9]\.)\b[0-9]+\b(?![.0-9])`, replace with `$&.0` – Wiktor Stribiżew Feb 27 '17 at 17:10
  • Can you explain this? Given `P3-4-5` it matches 4 and 5, and I'd like to ignore them but don't understand the expression. – Adam Feb 27 '17 at 17:20
  • In theory you have to match all the pragma's, define's, include's, or any custom MS directives. Then you also have to match all quotes and comments. Once you are matching that, you can tack on a sub expression to match numbers. And the numbers can be separated by whitespace. –  Feb 27 '17 at 17:22
  • See https://regex101.com/r/9J6HGv/1, there is an explanation on the right. – Wiktor Stribiżew Feb 27 '17 at 17:25
  • Why do you need to do this? What problem are you trying to solve in which this is the solution, or on the path to the solution? It sounds like an X/Y problem to be honest. – Lasse V. Karlsen Feb 27 '17 at 21:15

2 Answers2

1

Using the regex provided by @Wiktor Stribiżew

  1. Close all documents
  2. Open the problematic documents
  3. Ctrl+h
  4. Set search to regex
  5. Set scope to All Open Documents
  6. Search for (?<![0-9]\.)\b[0-9]+\b(?![.0-9])
  7. Replace with $&.0
  8. Replace all
  9. Review fix any strings that should not have been affected
Adam
  • 1,825
  • 1
  • 17
  • 24
1

It seems the pattern I suggested works in almost 99% of cases, so, let me explain the (?<![0-9]\.)\b[0-9]+\b(?!\.) pattern:

  • (?<![0-9]\.) - a negative lookbehind failing the match if there is a digit and a . symbols right before the digit matched with [0-9]+ pattern
  • \b - a leading word boundary (i.e. the previous char cannot be a letter/digit/underscore)
  • [0-9]+ - 1 or more digits
  • \b - a trailing word boundary (i.e. the next char after the last matched digit cannot be a letter/digit/underscore)
  • (?!\.) - a negative lookahead that fails the match if the next char after the last digit matched by the [0-9]+ subpattern is followed with ..

Why [0-9]+ instead of \d? See this SO thread, \d is Unicode aware and matches more than just 0..9 digits.

See the pattern demo.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I am not sure the `(?<![0-9]\.)\b[0-9]+\b(?![-\s.]|$)` regex will work in 100% cases either, it is just an "extension" of the one above: the lookahead fails the matches even when the digits are followed with `-` or whitespace (`[-\s]`), or if they are at the end of string (`$`). – Wiktor Stribiżew Feb 27 '17 at 21:21