Visual Studio replace magic number integers with doubles - regex

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

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.

Using the regex provided by #Wiktor Stribiżew
Close all documents
Open the problematic documents
Ctrl+h
Set search to regex
Set scope to All Open Documents
Search for (?<![0-9]\.)\b[0-9]+\b(?![.0-9])
Replace with $&.0
Replace all
Review fix any strings that should not have been affected

Related

Regex positive look behind notepad++

I'm trying to use regex to grab the last 3 digits of a string that varies in size. The beginning of the string always leads with name-
Examples:
"Name-0000425"
"Name-00123"
"Name-123"
I want the regex to grab the last 3 digits of this number and replace it with these digits. This is the regex I have tried but it only grabs the first 3 digits.
(?-s)(?<=Name-)(\d{3})
Any help would be appreciated! Thanks!
You can use
(?<=Name-)\d*(\d{3})
Replace with $1. See the regex demo.
Alternatively, you may also use (Name-)\d*(\d{3}) and replace with $1$2.
Details
(?<=Name-) - a positive lookbehind that matches a location immediately preceded with Name-
\d* - any zero or more digits
(\d{3}) - Group 1 ($1 refers to this value from the replacement pattern): three digits.
NOTES
If you only want to remove initial zeros, you can use the (?<=Name-)0+(\d{3}) regex.
If there can be anything else before the last three digits on the line, you may use (?<=Name-).*(\d{3}).
If there can be anything else before the last three digits that are inside double quotes on a single line, you may use (?<=Name-)[^"\n\r]*(\d{3}).
Shortest regex will be...
Find what: \d{3}"
Replace it with 789"

Regex to find where space is missing between number and word

I am using regex to clean some text files.
In some places, spaces are missing as in the second line below:
1.9 Beef Curry
1.10Banana Pie
1.11 Corn Gravy
I need an expression to find a zero-length match at the position between 0 and B, so that I can replace it (in Notepad++) with a space. Note that numerators can be one or two digits, and there can also be one (i.e. 1. Exotic Disches) or three levels (i.e. 2.5.1 Chicken).
Can someone please give the answer?
I would have thought one of the following should work, but Notepad++ calls it invalid. Would also appreciate it if someone can tell my why...
(?<=\.\d\d|\.\d)(?! )(?!\.)
(?<=\.\d{1,3)(?! )(?!\.)
Thanks in advance!
Maybe it is enough, just to look for the zero length spaces \B (non word boundaries) between word characters and check, if preceded by a digit and not followed by a digit. If so, replace with space.
\B(?<=\d)(?!\d)
See this demo at regex101
at any \B non word boundary
(?<=\d) looks behind for a digt
(?!\d) looks ahead for no digit
For further restricting the digit part to dot, followed by 1-3 digits, try something like \.\d{1,3}\B\K(?!\d) where \K resets beginning of the reported match. Or without \K and replace by $0
Just to mention: Also the underscore belongs to word characters. If your input contains underscores, e.g. something like 1_ and you don't want to add space here, change the lookahead to (?![\d_])
You may use one of
^\d[\d.]*+(?!\h)
^\d[\d.]*+(?! )
^(?>\d+(?:\.\d+)*\.?)(?!\h)
Replace with $& .
Settings and test:
Details
^\d[\d.]*+(?!\h) matches a digit and then 0 or more digits/dots and once they are all matched, a horizontal whitespace is checked for. If there is no whitespace, there is a match.
^\d[\d.]*+(?! ) is the same, just the check is performed for a regular space.
^(?>\d+(?:\.\d+)*\.?)(?!\h) is more specific, it matches
^ - start of line
(?>\d+(?:\.\d+)*\.?) - an atomic group preventing backtracking:
\d+ - 1+ digits
(?:\.\d+)* - 0 or more sequences of . and 1+ digits
\.? - an optional dot
(?!\h) - no horizontal whitespace allowed immediately on the right
My alternative attempt also working
Find what: ^(\d\.\d+) ?(?=\w)
Replace with: $1 a space after $1

Regex, match anything unless just numbers

I have got the following regex expression so far:
used-cars\/((?:\d+[a-z]|[a-z]+\d)[a-z\d]*)
This is sort of working, I need it to match basically ANYTHING apart from JUST numbers after used-cars/
Match:
used-cars/page-1
used-cars/1eeee
used-cars/page-1?*&_-
Not Match:
used-cars/2
used-cars/400
Can someone give me a hand? Been trying get this working for a while now!
There are few shortcomings of your regex used-cars\/((?:\d+[a-z]|[a-z]+\d)[a-z\d]*).
It's checking for used-cars/ followed by multiple digits then one character within a-z OR multiple characters within a-z then one digit.
[a-z\d]* is searching for either characters or digits which is also optional.
It's inaccurate for your pattern.
Try with following regex.
Regex: ^used-cars\/(?!\d+$)\S*$
Explanation:
used-cars\/ searches for literal used-cars/
(?!\d+$) is negative lookahead for only digits till end. If only digits are present then it won't be a match.
\S* matches zero or more characters other than whitespace.
Regex101 Demo

XML Regex - Negative match

I have a problem with negative lookahead in XSD pattern.
When I specified:
<xs:pattern value="^(?!(00|\+\d))\d{6,}$"/>
then I got an error message:
Value '^(?!(00|\+\d))\d{6,}$' is not a valid XML regular expression.
Any idea why it does not work?
In online javascript validator it works fine (e.g. here under unit tests section click on "run test").
I need to validate phone numbers. The phone number cannot include international prefixes (+\d) and (00).
Thanks
Try the following regex:
[1-9][0-9]{5,} | 0[1-9][0-9]{4,}
This matches a number which does not begin with zero and is followed by any digit (including zero) 5 or more times, and it also matches a number which starts with zero and is not immediately followed by zero, but after that can have 0-9.
I will add my deleted comment as an answer:
([1-9][0-9]|[0-9][1-9])[0-9]{4,}
See the regex demo.
The regex should work well for your scenario because
([1-9][0-9]|[0-9][1-9]) - matches either 1 digit from 1-9 ranges and any digit after or (|) any 1 digit followed with any digit but 0 - making up 2 digits
[0-9]{4,} - matches 4 and more any digits.
This pattern only matches a full/entire string because all regex patterns inside XSD pattern are anchored by default (so, you do not have to and can't enclose the pattern with ^ and $).
Right, there is no lookaround support in XSD regex (no lookaheads, nor lookbehinds). Besides, XSD regex has other interesting limitations/features:
^ and $ anchors
Non-capturing groups like (?:...) (use capturing ones instead)
/ should not be escaped, do not use \/
\d should be written as [0-9] to only match ASCII digits (same as in .NET)
Back-references like \1, \2 are not supported.
No word boundaries are supported either.
See some more XSD regex description at regular-expressions.info.

Regular expression to match last number in a string

I need to extract the last number that is inside a string. I'm trying to do this with regex and negative lookaheads, but it's not working. This is the regex that I have:
\d+(?!\d+)
And these are some strings, just to give you an idea, and what the regex should match:
ARRAY[123] matches 123
ARRAY[123].ITEM[4] matches 4
B:1000 matches 1000
B:1000.10 matches 10
And so on. The regex matches the numbers, but all of them. I don't get why the negative lookahead is not working. Any one care to explain?
Your regex \d+(?!\d+) says
match any number if it is not immediately followed by a number.
which is incorrect. A number is last if it is not followed (following it anywhere, not just immediately) by any other number.
When translated to regex we have:
(\d+)(?!.*\d)
Rubular Link
I took it this way: you need to make sure the match is close enough to the end of the string; close enough in the sense that only non-digits may intervene. What I suggest is the following:
/(\d+)\D*\z/
\z at the end means that that is the end of the string.
\D* before that means that an arbitrary number of non-digits can intervene between the match and the end of the string.
(\d+) is the matching part. It is in parenthesis so that you can pick it up, as was pointed out by Cameron.
You can use
.*(?:\D|^)(\d+)
to get the last number; this is because the matcher will gobble up all the characters with .*, then backtrack to the first non-digit character or the start of the string, then match the final group of digits.
Your negative lookahead isn't working because on the string "1 3", for example, the 1 is matched by the \d+, then the space matches the negative lookahead (since it's not a sequence of one or more digits). The 3 is never even looked at.
Note that your example regex doesn't have any groups in it, so I'm not sure how you were extracting the number.
I still had issues with managing the capture groups
(for example, if using Inline Modifiers (?imsxXU)).
This worked for my purposes -
.(?:\D|^)\d(\D)