5

To add more precision to floating point numbers, I have to go through a bunch of C# classes and, wherever there is a division operation, multiply both the numerator and denominator by 1000. So suppose we have a numerator, n and a denominator, d. Then the original would look like this: n / d. And I would need to change it to (n*1000)/(d*1000).

To search for all the division operators I'm using the find function (ctrl+f) in Visual Studio 2013 Ultimate. I'm searching for all '/'. The problem is that the find function also picks up the slashes in the comments. How can have the find function only find the single slash '/' and not the double slash '//'?

Thank you for any suggestions.

navig8tr
  • 1,724
  • 8
  • 31
  • 69

4 Answers4

5

Good

Regex find (Ctrl+Shift+F with the Use Regular Expressions option checked) [^/<]/[^/>] in your solution - basically a / that:

  • is not preceded by another / or a < (i.e. closing XML comment tags like </para>)
  • is not followed by another / or a > (i.e. standalone XML comment tags like <see cref="Foo"/>)

The only false positives I see with this approach in a quick check against a solution of ~80 projects are...

  • Dates (e.g. 1/1/2014)
  • / in comments (e.g. Note that an up/down change....)

...but others could come up depending on your code base - for example:

  • URLs (e.g. http://stackoverflow.com/users/1810429/j0e3gan)
  • XPaths (e.g. /Customer/Address/City/text())

Better

If you consistently put spaces between the division operator and its operands, you could tighten the regex a bit to alleviate these false positives, changing it from...

[^/<]/[^/>]

...to...

[^/<] +/ +[^/>]

...where + will match one or more spaces of course.

mx0
  • 6,445
  • 12
  • 49
  • 54
J0e3gan
  • 8,740
  • 10
  • 53
  • 80
2

Try this regex expression:

[^/<]/[^/>]

"Not a slash followed by a slash followed by not a slash"

mclaassen
  • 5,018
  • 4
  • 30
  • 52
2

You can find single / in VS2013 with the following using :

  • type Ctrl+f
  • in the search box, type (?<!/)/(?!/)
  • under the search box, click on the icon representing .*
  • select Entire Solution
  • Launch the search

But, you must know that the result will be not what you really want, because, you will find XAML code, URL, ...

Xaruth
  • 4,034
  • 3
  • 19
  • 26
  • Yes it will find xaml code, but at least it doesn't pick up the comments. Thanks for the suggestion. – navig8tr May 12 '14 at 17:30
1

A regular expression search for (?<!/)/(?!/) should find only single slashes, by using zero-width negative lookbehind/lookahead assertions. The .* button in the small find dialog turns on regular expression searches. There is a checkbox under "Find options" for the full search dialog (Ctrl+Shift+F).

Mark
  • 8,140
  • 1
  • 14
  • 29
  • This just dresses up @mclaassen's so-so regex with a negative lookbehind & lookahead - same so-so results and needlessly complicated by a negative lookbehind & lookahead. – J0e3gan May 12 '14 at 17:27
  • @J0e3gan Well, in my defense, I answered first, and not being psychic I wasn't to know that I wasn't adding anything to the (non-existent) previous answers. While I am happy to be corrected, you may want to work on your delivery. – Mark May 12 '14 at 19:02