Regex to Match the first 6 digits of a phone number - regex

ok guys I have a list of Phone Numbers like:
2017582043
3928530146
2791047392
7684038294
and the list goes on an on
what I am trying to do is loop thru the list returning the first 6 digits of each number
May somebody help me figure out that it the correct regex for that?

To get the first 6 digits you just have to do ^\d{6} for each line. This is for python so your syntax may change based on your language.

If all that you want is to get the first 6 digits, then do not use regex for that, depending on the language, you can extract the first 6 characters easily from a string, using one of the string manipulation functions. Or if the language stores the string as an array of characters, it is even easier. I presume that the first 6 characters are always digits.
Which language are you working with?

^ = start, \d = number, {6} = 6 times.
/^\d{6}/

What you want is something like this /^(\d{6})\d+/ and you can then use $1 as your replacement.
See regular expressions: match x times OR y times which is a similar but more complex issue.

Related

Regex extract first portion of four numbers starting from specific position

How do i extract four numbers starting after the 8th number which is dynamic from the following strings using regex.
20190715171712904_10008_file_activate_10.20.30.4000233223456_name.unl
20190715141712904_10008_runco_activate_10.20.30.40_name.unl
From first string i want 1717
From second string i want 1417
I have tried to write regex queries in https://regex101.com/ i.e.
I have tried ^\d{8}([0-9]{4})$ but not working.
Drop the $. It forces the expression to look for the end of the string after your 4 digits, which it is not. The answer will be in the first subgroup capture. Note you can use \d for the second [0-9] as well.
If your language supports look-behinds, you can capture your digits as the main capture, instead of a subgroup:
(?<=^\d{8})\d{4}
This is really not a problem for a regular expression though - getting the substring indexed from index 4 to index 7 including (0 indexed) is basic and faster in any language.

Verifying that a string starts with a number (easy) OR exactly 3 letters?

I'm trying to make a RegEx expression to verify that a field starts with either the number 3 - the easy part - or starts with three letters, then continues to be numbers
My expression so far is
^((3)[\d])|([a-zA-Z]{3}[\d])$
The expression stops you from doing anything BELOW 3, but it still lets you go over...
I've done some searching and can't find a topic that relates to the issue of having an exact amount of characters
And I'm having trouble with limiting it to exactly 3 letter characters. Unfortunately what I'm working with, it HAS to be RegEx and not another language.
^(?:3|[a-zA-Z]{3})\d+$
verifies, that your string starts with either 3 or 3 letters and then is only followed by numbers (at least one) until the end of the string
See https://regex101.com/r/tD2nK4/3 for some positive and negative examples
This regex should do exactly what you want:
^((3)[\d])|([a-zA-Z]{3}[^a-zA-Z])
Please note that this regex can only cope with the ASCII alphabet.

Regular expression starting at least with 2

I am trying to get/make a regular expression but i can't figure it out. I am searching for an expression so that a user, who is filling a form, can't type 0 ore 1. So it has to start at least with 2. What is the expression for it?
Thanks a lot.
Thanks. But this is not 100% waterproof. As a user you can't fill 0 or 1 but you can't fill 10 or 11 or 101 either. So everything with a 0 or a 1 at the beginning. Is there a solution?
Thanks again.
here, this should accept any numbers starting with 2 or more:
[2-9][0-9]*
or
^[2-9][0-9]*$
if you are matching whole lines.
I understand you mean it begins with a digit from 2 to 9, but you should tell if it can contain else later.
for pure numbers:
[2-9][0-9]*
this forces the content be numeric ans start with a digit > 1.
Use:
[2-9][0-9]+
if more than one number is mandatory,
This works as exact match, if you are doing a non-exact match use anchoring:
^[2-9][0-9]*$
if after the initial digit different character can happen use an appropriate pattern e.g:
[2-9].*
matches anything after the first digit:
[2-9][0-9a-zA-Z]*
matches a alfanumeric pattern etc...
If you mean to accept any string that is an integer number bigger than 1:
([1][0-9]+|[2-9][0-9]*)
the first half ([1][0-9]+) will match a number starting by 1 followed by at least another digit, the second will match the numbers 2-9 or a number starting with a digit 2-9 and more figures ([2-9][0-9]*).
Note that this does not accept potentially good integers written with a leading 0, like 0123. If you want to include that as well use:
(0*[1][0-9]+|0*[2-9][0-9]*)
Also note that a pattern like:
(matcher1|matcher2)
is not supported by all RE engines.
I reckon something like this would be useful for you:
(2+)(.)*
It's mean that only words starting with "2" math the expression.
If you wanna try regular expressiona easely, i like the web http://rubular.com/
It has a good interface to test expressions directly onto the web.
Greetings

Regex : Find a number between space

I am trying to extract a zip code of six numbers starting with the number 4 from a string. Right now I am using [4][0-9]{5}, but it is also matching starting from other numbers, like 020-25468811 and it's returning 468811. I don't want it to search in the middle of a number, only full numbers.
Try to use the following:
(?<!\d)4\d{5}(?!\d)
I.e. find 6-digit number starting with 4 and not preceded or followed by digit.
Your expression right now tries to match any six numbers consisting of a 4 with five numbers between 0 and 9. To fix this behavior you should add word boundaries as per Jon's suggestion.
\b[4][0-9]{5}\b
More on word boundaries here: http://www.regular-expressions.info/wordboundaries.html
You could simply add a space to the beginning of your regular expression " 4[0-9]{5}". If you need a more universal way of finding the beginning of the number (could it maybe be also be tabulator, a newline, etc?) you should have look at the predefined character class \s. Also have a look at boundary matchers. I dont know which language you are using, but regex work very similar in most languages. Check this Java regex documentation.
There is a start of line character in regex: ^
You could do:
^4[0-9]{5}
If the numbers are not always in the beginning of a line, you can more generally use:
\<4[0-9]{5}\>
To match only whole words.
Both examples work with egrep.

RegEx failing for strings with less than 3 characters

I am using a RegEx to test if a string is valid. The string must start and end with a number ([0-9]), but can contain comma's within.
I came up with this example, but it fails for strings less than 3 characters (for example 1 or 15 are as valid as 1,8). Presumably this is because I am specifically testing for a first and last character, but I don't know any other way of doing this.
How can I change this RegEx to match my requirements. Thanks.
^[0-9]+[0-9\,]+[0-9]$
Use this:
^[0-9]+(,[0-9])?$
the ,[0-9] part will be optional
visualized:
if you want allow for multiple comma-number groups... then replace the ? with *.
if you want to allow groups of numbers after the comma (which didn't seem to be the case in your example), then you should put + after that number group as well.
if both of the above mentioned are desired, your final regex could look like this:
^[0-9]+(,[0-9]+)*$
^\d+(?:,\d+)*$
should work.
Always have one or more digits at the start, optionally followed by any number of comma-separated other groups of one or more digits.
If you allow commas next to each other, then the second + should be a *, I think.
I would say the regex
\d(,?\d)*
Should satisfy for 1 or more digits that can be separated by only one comma. Note, 1,,2 fails