Help with regex to find numbers - regex

I'm trying to find all single numbers (with the use of vim):
numbers at start of line
numbers at end of line
the number has to be followed and proceded by a non number
but may not be folowed or proceded with a "dot" and a number or a "," and a number.
this is correct
7
word7
7word
7.
.7
a,7
word7word
word 7 word
7-7
but not this
7.7
7,7
77
Can anyone help me and explain the regex?
EDIT:
may'be I've found it with the help of an answer below about atomic grouping. Vim does support it:
\(\d\.\|\d\,\|\d\)\#<!\d\(\.\d\|\,\d\|\d\)\#!

You can try this:
\v%(\d+%(\.|,))#<!\d#<!\d+#>%(%(\.|,)\d)#!
Explanation:
\v turns very magic : no need of many backslashes
the % signs are optional (make groups in parentheses non-matching)
(\d+(\.|,)#<! : not preceded with digits then . or ,
\d#<! : not preceded with a digit (be sure we are at the first digit
\d+#> : consume all digits (#> ensures that, see :help /\#>)
((\.|,)\d)#! : after that, no dot or comma followed by a digit.

Give this a whirl:
^(?!\d(\.|\,)?\d)(((\D*?)\d(\D*?))|(\d(\D*?)\d))$
And let me know if you'd like an explanation.

Try this one:
\(\d[\.,]\)\#<!\d\#<!\d\d\#!\([\.,]\d\)\#!
Explanation:
It looks for digits (\d) that are not preceeded by a '.' or ',' followed by a digit (\(\d[\.,]\)\#<!) or a single digit (\d\#<!), and is not followed by '.' or ',' followed by a digit (\([\.,]\d\)\#!) or a single digit (\d\#!).
This one is straight from my vim so it should work in yours.

Related

a regex to add a period inbetween numbers

im in need of help regarding a regex,
i have lines that look like this
1995
80
100
83
3
Etc
I need them to becomes
1.9.9.5
8.0
1.0.0
8.3
3
and i dont know how the single numbers would turn out with a regex to do this
I dont have experience with regex, which is why im posting here, any help is greatly appreciated, thank you
Use lookaround to do such job.
Ctrl+H
Find what: (?<=\d)(?=\d)
Replace with: .
Replace all
Explanation:
(?<= : start lookbehind, make sure before we have
\d : a digit
) : end lookbehind
(?= : start lookahead, make sure after we have
\d : a digit
) : end lookahead
Replacement:
a dot, that will be inserted between 2 digits.
Result for given example:
1.9.9.5
8.0
1.0.0
8.3
3
Find what: (?=\d\d+)(.)
Replace with: \1\.
uses a non consuming group to first assert that the line has more than one digit,
then captures each digit and replaces it with the digit followed by a .
for the last digit in a number with more than one ie 234, it fails the assertion because the regex engine has already parsed 234 and so treats it as 4.

Regex - matching while ignoring some characters

I am trying to write a regex to max a sequence of numbers that is 5 digits long or over, but I ignore any spaces, dashes, parens, or hashes when doing that analysis. Here's what I have so far.
(\d|\(|\)|\s|#|-){5,}
The problem with this is that this will match any sequence of 5 characters including those characters I want to ignore, so something like "#123 " would match. While I do want to ignore the # and space character, I still need the number itself to be 5 digits or more in order to qualify at a match.
To be clear, these would match:
1-2-3-4-5
123 45
2(134) 5
Bonus points if the matching begins and ends with a number rather than with one of those "special characters" I am excluding.
Any tips for doing this kind of matching?
If I understood requirements right you can use:
^\d(?:[()\s#-]*\d){4,}$
RegEx Demo
It always matches a digit at start. Then it is followed by 4 or more of a non-capturing group i.e. (?:[()\s#-]*\d) which means 0 or more of any listed special character followed by a digit.
So just repeat a digit, followed by any other sequence of allowed characters 5 or more times:
^(\d[()\s#-]*){5,}$
You can ensure it ends on a digit if you subtract one of the repetitions and add an explicit digit at the end:
^(\d[()\s#-]*){4,}\d$
You can suggest non-digits with \D so et would be something like:
(\d\D*){5,}
Here is a guide.

Sublime colour syntax highlighting using regex

I've managed to put together a syntax (.tmLanguage) file for use in Sublime Text 2. I'd quite like to highlight numerals. I tried:
<string>0|1|2|3|4|5|6|7|8|9</string>
which works, but only for single digits, so I thought the regex would be
<string>[0-9]</string>
But that doesn't work. Can someone please help me with the correct syntax in Sublime?
If you change your code to:
<string>\d+</string>
It should find all integers.
\d equals any number (0-9)
+ Is a multiplier stating "one or more of the previous character"
In your case, at least one digit, but as many as possible. Might I suggest:
<string>\d+(\.\d+)?</string>
as that will find decimal numbers as well.
\d equals any number (0-9)
+ Is a multiplier stating "one or more of the previous character"
( Starts a group
\. An escaped period sign, to actually capture the period character
\d+ One or more digits
) End f the group
? Makes the entire group optional.
That should capture both integers and decimal numbers.

Regex to match "[any single digit number]" and ":any four digit number/"

I require the regex to match the following pattern in vbscript
[any single digit number] and :any four digit number/
That is, a open square bracket followed by a single digit number followed by a close square bracket or a colon followed by four digit number followed by a slash
I've tried the following pattern which select just a single digit also
[\[]*([:]*([0-9]{1,4})[/]*)[]]*
Thanks in advance,
Madhan
Try like this:
(\[\d\]|\:\d{4}\/)
Live Demo
This should work:
(\[\d\]|:\d{4}/)

Need regex to match 1 or more of exactly n-digit numbers

I need a regex to match a series of one or more n-digit numbers, separated by comma, ie:
abc12345def returns 12345
abc12345,23456def returns 12345,23456
so far I got this: \d{5}(,\d{5})*
problem is it also matches in cases like these:
123456 returns 12345, but I need it not to match if the number is longer than 5. So I need numbers of exactly 5 digits, and if a number is shorter or longer it's a no-match
Thanks
Which language are you using for your regexes? You want to put non-digit markers around your \d{5}'s; here is the Perl syntax (with a negative look-ahead/look-behind fix by Lukasz):
(?<![\d,])\d{5}(,\d{5})*(?![\d,])
Actually I think I got it! (?<!\d)\d{5}(?!\d)(,(?<!\d)\d{5}(?!\d))*
I used the look-ahead and look-behind
Thanks.
You could use this one:
/\D?\d{5}(?:,\d{5})?\D?/
explanation:
/ : regex delimiter
\D? : non digit optionnal
\d{5} : 5 digits
(?: : begining of non-capture group
,\d{5} : comma and 5 digits
)? : end of group optionnal
\D? : non digit optionnal
/ : regex delimiter