Regex - Keep all digits with length of 10-13 digits - regex

search for regex where Keep all digits with length of 10-13 digits and delete the rest in notepad++
my regex doesnt work
[^\d{10,13}]
it finds numbers with commas too :(

Searching for
^(?:.*?(\d{10,13}).*|.*)$
and replacing with
\1
you keep just the 10 to 13 digit long numbers (and empty lines).
Remove the empty lines searching for
^\n
and replacing with nothing.
See it in action: RegEx101.
Addressing #WiktorStribiżew's comments: Relying on the sought after numbers to be always surrounded by white space (which has been checked with OP - but not for the potential case, lines to (effectively) hold just numbers) the search expression could be adjusted to
^(?:.*\s(\d{10,13})\s.*|.*)$
still replacing with
\1
to handle comma holding strings of numbers correctly: RegEx101
By the way:
[^\d{10,13}]
is a character class, which matches anything, which is not:
a number, or
any character out of "{10,3}" (without the quotes, but including the curly braces).
Please comment if and as this requires adjustment / further detail.

To match numbers that are not exactly 3 digits long:
\b(\d{1,9}|\d{14,})\b

You can find all 10-13 length stand alone digits like this
(?<!\d)\d{10,13}(?!\d)
What you do then is up to you.

I don`t know how does notepad works, but this I think this is the regex you are looking for: ^([0-9]){10,13}$
A good page to create/test regex: http://regexr.com/

Related

regex floating numbers or integers only

I am trying to clean up a field so that it only has integers or floating numbers.
Basically, I want to the row to be blank if there are dates or text.
This catches most things:
regex_replace("^(\d*.\d*).*","$1")
but leaves the initial numbers if the row is a date (i.e. 2022 if 2022-07-01 20:30:29 or 7 if 7/1/2022).
0
0
1
15
1.8910127482598
2022-07-01 20:30:29
7/1/2022
West
Living
C000000475
1
0
0
0
How can I modify the regex so that it removes the dates as well?
TIA,
LCH
Find all numbers
^(\d*\.?\d*)$
Your regular expression uses \d.\d which is probably not intended by you. The . must be escaped, otherwise it will be interpreted as "any character".
Notice I wrote \.? to find an optional decimal point. A . means "any character", not the decimal dot. We therefore escape it.
I added the $ at the end to denote "end of line".
Replacing with $1 just leaves the number. Use an empty string to remove numbers.
Find a playground on regex101 here:
https://regex101.com/r/P7jwNV/1
This is a slightly tweaked version of your expression. However, it will go through the lines and replace the number with themselves. How would that leave the other rows empty?
Remove the numbers
You say you want to remove the non-numbers, however your regular expression is trying to find numbers and replace them with the full search result. Which is the same as not doing anything.
^([^:\-\/\D]+|\d+\.\d+)$
With your examples this will leave the non-numbers if we replace with $1.
See regex101 playground here:
https://regex101.com/r/VV68Pj/1
Remove the non-numbers
Regular expressions are not for finding a pattern you then want the opposite of the matches to work on. Se we have to find the patterns we don't want to replace them with an empty string. We can classify the non-numbers separately with |:
^((?![\d]+).+|\d+\/\d+\/\d+|\d+-\d+-\d+ \d+:\d+:\d+)$
?! is a negative lookahead, in our case it finds a non-digit
(?![\d]+).+: If the following does not have a digit in it...
\d+\/\d+\/\d+: Or the following is a date (I escaped / there, you may not need to)...
\d+-\d+-\d+ \d+:\d+:\d+: Or the following is a date + timestamp
We then simply replace with nothing (an empty string) to remove them.
Regex101 playground to tinker with it:
https://regex101.com/r/ZK5PDZ/1

Regex to delete everything behind first letter

I have a regex \b\d+\K[a-z] Replace with: \u$0
This makes letters in front of numbers caps, for example:
123host
1643domain
into
123Host
1643Domain
What I need to figure out now is how can I delete the numbers.
So I need:
123host
to become
host
and so on, all entries have a numbers in front of them like this:
6410james
599stacks
Into
james
stacks
I tried doing \b\d+\K[a-z] replace with nothing, but it just deletes the first letter, I'm a total noob and any help would be appreciated.
You can simply find \d+ or [0-9]+ and replace it with an empty string, if all samples have the digits in the start. ^\d+ or ^[0-9]+ would also work fo our cases, however it would not work if we'd have digits after the letters.
The expression is explained on the top right panel of this demo if you wish to explore/simplify/modify it.
The pattern you probably want to search for is:
^[^a-zA-Z]*
and then replace with empty string. This is a literal translation of the requirement to remove every non letter from the start of the string.
Demo

Regex to match text and a specific number

I am looking for a regex which can match the following conditions.
It always starts with "someId":[ and ends with ].
It must contain the number 25 within the square brackets.
There may be numbers before and after number 25
The numbers are separated with a comma (,) apart from the last number
For example:
"someId":[25]
"someId":[25,27]
"someId":[1,4,25]
"someId":[1,4,25,27,30]
I have the following regex which works, however I was wondering if theres a better way to do it which isn't as greedy.
"someId":\[(\d{1,2},)*?25,?(\d{1,2},)*?(\d{1,2})?]
a bit simplified:
"someId":\[(\d+,)*25(,\d+)*\]

Regex a decimal number with comma

I'm heaving trouble finding the right regex for decimal numbers which include the comma separator.
I did find a few other questions regarding this issue in general but none of the answers really worked when I tested them
The best I got so far is:
[0-9]{1,3}(,([0-9]{3}))*(.[0-9]+)?
2 main problems so far:
1) It records numbers with spaces between them "3001 1" instead of splitting them to 2 matches "3001" "1" - I don't really see where I allowed space in the regex.
2) I have a general problem with the beginning\ending of the regex.
The regex should match:
3,001
1
32,012,111.2131
But not:
32,012,11.2131
1132,012,111.2131
32,0112,111.2131
32131
In addition I'd like it to match:
1.(without any number after it)
1,(without any number after it)
as 1
(a comma or point at the end of the number should be overlooked).
Many Thanks!
.
This is a very long and convoluted regular expression that fits all your requirements. It will work if your regex engine is based on PCRE (hopefully you're using PHP, Delphi or R..).
(?<=[^\d,.]|^)\d{1,3}(,(\d{3}))*((?=[,.](\s|$))|(\.\d+)?(?=[^\d,.]|$))
DEMO on RegExr
The things that make it so long:
Matching multiple numbers on the same line separated by only 1 character (a space) whilst not allowing partial matchs requires a lookahead and a lookbehind.
Matching numbers ending with . and , without including the . or , in the match requires another lookahead.
(?=[,.](\s|$)) Explanation
When writing this explanation I realised the \s needs to be a (\s|$) to match 1, at the very end of a string.
This part of the regex is for matching the 1 in 1, or the 1,000 in 1,000. so let's say our number is 1,000. (with the . on the end).
Up to this point the regex has matched 1,000, then it can't find another , to repeat the thousands group so it moves on to our (?=[,.](\s|$))
(?=....) means its a lookahead, that means from where we have matched up to, look at whats coming but don't add it to the match.
So It checks if there is a , or a . and if there is, it checks that it's immediately followed by whitespace or the end of input. In this case it is, so it'd leave the match as 1,000
Had the lookahead not matched, it would have moved on to trying to match decimal places.
This works for all the ones that you have listed
^[0-9]{1,3}(,[0-9]{3})*(([\\.,]{1}[0-9]*)|())$
. means "any character". To use a literal ., escape it like this: \..
As far as I know, that's the only thing missing.

RegEx padding numbers surrounded by other characters

I am looking for a RegEx that captures a series of digits surrounded by a string pattern and pads that series of digits with leading zeros up to 4 digits. At the same time all spaces should be removed from the entire string.
Some examples:
"F12b" should capture "12" and return "F0012b"
"AB 214/3" should capture "214" and return "AB0214/3"
"G0124" should capture "0124" and return the original string unchanged
The source string should adhere to the following rules:
- should start with [a-zA-Z]
- after the above pattern can be any number of optional spaces
- the numeric sequence can be followed by another string
- the numeric sequence can be any number of digits. Only if there are less than 4 digits is the sequence to be padded with leading zeros, otherwise it remains unchanged.
- I am only interested in the first occurrance within a string
I am posting this question here because I don't use RegEx often enough to figure this one out, but I know it's a perfect case for RegEx.
Any help is greatly appreciated, and an explanation of the expression would certainly help me understand it.
To match that and extract the info you want, regex is fine, you can use this:
^([a-zA-Z]+)\s*(\d+)(.*)
See it here on regexr. You see only that the space has been removed in your second example, but all needed information is captured in $1, $2 and $3
Regular expressions are a tool to match patterns. Using that pattern within a replacement method and how the replacement string can be build is completely language dependent and has nothing to do with regex. Without knowing the language this part can not be answered.