How to cut last digits from number - REGEX - regex

I have to find the first 11 digits and cut everything that follows from the eleventh digit.
I've been trying to do it with this pattern :/^(\d{11}.*?). However, doesn't work.
You know what I'm doing wrong?

Depending on your regex flavour, you could use:
Find: ^\d{11}\K.+$
Replace: NOTHING
Explanation:
^ : beginning of line
\d{11} : 11 digits
\K : forget all we have seen until this position
.+ : 1 or more any character
$ : end of line

If you want to match first characters, you need to use anchor ^ that will anchor match at the beginning of the string.
If you want to match something and then reuse it, then you need to capture it isnide capturing group and use it in sbstitution with \1.
If you want to capture eleven digits - \d{11} will work for you.
So to sum up, you need pattern ^(\d{11}).* and replace with \1. .* will match 0 or more characters (any).

After lot of trying, It actually works with this one:
^(?=(\d{11})).+?

Related

Find the first set of 5 digits in a text

I need to find the first set of 5 numbers in a text like this :
;SUPER U CHARLY SUR MARNE;;;rte de Pavant CHARLY SUR MARNE Picardie 02310;Charly-sur-Marne;;;02310;;;;;;;;;;;;;;
I need to find the first 02310 only.
My regex but it found all set of 5 numbers :
([^\d]|^)\d{5}([^\d]|$)
To match the first 5-digit number you may use
^.*?\K(?<!\d)\d{5}(?!\d)
See the regex demo. As you want to remove the match, simply keep the Replace With field blank. The ^ matches the start of a line, .*? matches any 0+ chars other than line break chars, as few as possible, and \K operator drops the text matched so far. Then, (?<!\d)\d{5}(?!\d) matches 5 digits not enclosed with other digits.
Another variation includes a capturing group/backreference:
Find What: ^(.*?)(?<!\d)\d{5}(?!\d)
Replace With: $1
See this regex demo.
Here, instead of dropping the found text before the number, (.*?) is captured into Group 1 and $1 in the replacement pattern puts it back.
I would've use
(^(?:(?!\d{5}).)+)(\d{5})(?!\d)
It finds fragment from beginning of the string till end of first 5-digit number, but in case of replacement you can use $1 or $2 to substitute corresponding part. For example replacement $1<$2> will surround number by < and >.
To find the first 5 digits in the text, you could also match not a digit \D* or 1-4 digits followed by matching 5 digits:
^(?=.*\b\d{5}\b)(?:\D*|\d{1,4})*\K\d{5}(?!\d)
^ Start of string
(?=.*\b\d{5}\b) Assert that there are 5 consecutive digits between word boundaries
(?:\D*|\d{1,4})* Repeat matching 0+ times not a digit or 1-4 digits
\K\d{5} Forget what was matched, then match 5 digits
(?!\d) Assert what followed is not a digit
Regex demo

Match numbers after first character

I'd like to use Regex to determine whether the characters after the first are all numbers.
For example:
A123 would be valid as after A there are only numbers
A12B would be invalid as, after the first character, there is another letter
I essentially want to ignore the first character
I have so far this:
(?<=A)\w*(?=)
but this makes A12B or A1B2C valid, I only want numbers after A.
You could match not a digit \D, followed by matching 1+ times a digit. If that is the whole string, you could use anchors asserting the start ^ and the $ end of the string.
^\D\d+$
That will match:
^ Start of the string
\D Match not a digit
\d+ Match 1+ digits making sure there are digits
$ End of the string
Regex demo
The best solution I can think of is:
^.\d*$
^ - Start of the line
. - Any character (except line terminators)
\d*
\d- a number
* - repeated any number of times (including 0 times. If you want it to be at least 1, change it to +).
$ - End of the line
let regex = /^.\d*$/;
let testStrings = ['A123', 'A12B'];
testStrings.forEach(str => {
console.log(`${str} is ${regex.test(str) ? 'valid' : 'invalid'}`);
});
Your attempt is very complicated, especially given how simple is your goal.
Succeeding at regexes is all about simplicity.
The first character can be anything, so just go with ..
The next ones are all digits, so you want \d.
You'll star it to specify restriction-less repetition, or use + if you want at least one.
Finally, you need to anchor your regex at the beginning and at the end, else it would match stuff like A123XXXXX or XXXXA123.
Note that most implementations of match will already anchor the pattern at the end, so you can omit the caret at the beginning.
Final regex:
^.\d*$
Maybe
(?<=.{1,1})([0-9]+)(?=\s)
(?<=.{1,1}) - has exactly one character before
([0-9]+) - at least one digit
(?=\s) - has a whitespace after
Add ^ at the beginning - to specify beginning of line
Replace (?=\s) with $ for end of line
^[a-zA-Z][0-9]{3}$
^ - "starting with" (Here it is starting with any letter). Read it as ^[a-zA-Z]
[a-z] - any small letters and A-Z any capital letters (you may change if required.)
[0-9] - any numbers
{3} - describes how many numbers you want to check. You have to read it as [0-9]{3}
$ - End of the statement. (Means, in this case it will end up with 3 numbers)
Here you can play around - https://regex101.com/r/mqUHvP/5

Regex : must end with the first match of exaclty 10 digits between dashes

2017-34-5-1503650477-547-1234567890-coco.jpg
I want to match 2017-34-5-1503650477-
2017-34-5-1503650477-toast.jpg
I want to match 2017-34-5-1503650477-
2017-240-1503650477-toast.jpg
I want to match 2017-240-1503650477-
I'm trying to use /\b^(.*)\-\d{10}\-\b/ but on the first example it matches 2017-34-5-1503650477-547-1234567890- wheras I want to stop here : 2017-34-5-1503660477-
You should add the laziness modifier:
Note the question mark in (.*?)
\b^(.*?)\-\d{10}\-\b
Here is a regex101 example:
https://regex101.com/r/nQfsE9/1
You could use:
^(\d{1,9}-)+\d{10}
Where:
(\d{1,9}-)+ pairs of 1-9 digits and -
\d{10} followed by 10 digits
example

Select digits on the end of line

I need to replace only digits at the end of line with semicolon ; using RegEx in Notepad++.
Before:
ddd 66 ffff 5
d 44 dds 55
After:
ddd 66 ffff;
d 44 dds;
I'm trying to find digits at the end of lines with expression
($)(\d+)
but Notepad++ can't find anything by use of this expression. How to achieve this?
Find:
\s\d+$
Replace:
;
\d+ will match one or more digits. $ will match the end of the line--this is non-capturing (so don't worry... the end of the line will not be replaced in a find/replace operation). And so \d+$ will match one or more digits immediately followed by the end of the line.
I included \s (a single whitespace character) because it looks like you want to replace the space preceding the digits as well.
Note that you will need to do "Replace All" for this to work like you want. (because each regex match is for one instance only)
Try this find/replace:
find:
^(.*) \d+$
replace:
\1;
The find regex above matches anything up to and excluding a final space followed by at least one digit. If the end pattern for a given line is not space followed by one or more digits, the regex should not match. The replacement is the capture group, what is in parenthesis, which is everything up to but excluding the final space and number.

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