Regular expression starting at least with 2 - regex

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

Related

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.

MFC (CAtlRegExp) regular expression to match a string of N bytes valued 0 or 1 in OR

I have a string of, say for example, 6 bytes; each can be valued '0' or '1'. Which is the CAtlRegExp regular expression that matches any 0/1 combination of these bytes but not all 0s?
For example:
0001010 Match
0000001 Match
0000010 Match
1111111 Match
0000000 No match
I thought something like
^[01][01][01][01][01][01]$
but this one matches also "000000"
And in POSIX regular expression?
Thanks in advance
If, as you imply in the question, no other input is possible than a string mixed of zeroes and ones, all you have to is to find a one, or a non zero character, i.e. 1 or [^0]. This will give you a match if a one is found.
If your regex flavor supports look ahead, and you want syntax checking in the regex as well, you could try
(?=[01]{6})0*1
This does a positive look ahead to ensure 6 (change to length wanted) zeroes or ones, and then checks that a one, possibly prefixed by any number of zeroes, follows.
EDIT: Checked out CAtlRegExp and it doesn't support look ahead :(. But I've got an even simpler regex that probably does the job:
{0*1+[01]*}
This doesn't verify the length, but it should satisfy your other needs. Also captures the string, if that's what you want (CAtlRegExp flavor). If not, remove the braces.
Hope this helps.
Regards

Regex to Match the first 6 digits of a phone number

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.

Regex for detecting numbers away from each other?

so basically I want to detect if in these strings:
Hello 123 My 222 dear 112 troll 12 8889
192.1.1.254:10000
the numbers are in a format like this:
[0 to 255][ANYTHING][0 to 255][ANYTHING][0 to 255][ANYTHING][0 to 255][ANYTHING][0 to 65536]
Does anyone know how I can build such a regex?
It is for detecting if anyone posts an IP:Port in unusual format to bypass default ip:port filters.
Edit: As for the first comment: I do not know regex and what I have tried is:
if(regex_match("192.168 najlepszy serwer SAMP!!1 1 join1!! 8080","/^[0-2](*)?[0-5](*)?[0-5](*).(*)[0-2](*)?[0-5](*)?[0-5](*).(*)[0-2](*)?[0-5](*)?[0-5](*).(*)[0-2](*)?[0-5](*)?[0-5](*)?$/"))
{
print("Cannot send message");
}
else
{
print("New message for everyone! :)");
}
and some other not working regexes.
If you don't want to complicate your life checking the exact ranges, the simple regex would be:
/^.*(\d)+.+(\d)+.+(\d)+.+(\d)+.+(\d)+.*$/
The first four (\d)+ parts can be replaced with more complicated check for 0-255 range:
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
the last (\d)+ replace with next for port range check:
(6553[0-5]|655[0-2]\d|65[0-4]\d\d|6[0-4]\d\d\d|[1-5]\d\d\d\d|[1-9]\d{0,3})
An exact, simple, and direct representation of your pattern as a regular expression is not possible in the general case. The reason are the number ranges. Something like "at this place any integral number with a value from a to b" is just to complex. A regular expression is executed by a finite state machine and these (theoretical) beasts are (basically) only able to look at strings character by character. Therefore you can match something like "ignore all characters until you find the first digit, then check whether the first digit is followed by at most two more digits".
As a workaround you may try to build a list of alternations of possible digit patterns that covers your desired range of values (in the extreme case list every single value like \b(?:1|2|3|4|...|154|155|...|255)\b). I have a pattern for the range 0-255, but I have none for the range of possible port numbers. So a first approximation may be (really, this is only an approximation and not thoroughly tested):
\b(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b.*\b(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b.*\b(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b.*\b(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b[^0-9]*[0-9]{1,5}
In the above pattern (?: .... ) means a shy group (not remembered for back references) and \b means word boundary.
I'd suggest you read up on Regex syntax. For starters . is special and matches any character. Also doing something like [0-2][0-5][0-5] won't catch something like 192 as 9 is not within 0-5.
According to your requirements here's a Regex that should roughly do what you want
([0-2]?\d{1,2}).*([0-2]?\d{1,2}).*([0-2]?\d{1,2}).*([0-2]?\d{1,2}).*(\d{1,5})?
Each of the ([0-2]?\d{1,2}) portions will match 1 or 2 digits preceded optionally with a 0,1, or 2. Each () will capture a group which you can then examine using a Regex engine. You will need to examine this group as the Regex for each of those portions will match numbers above 255 (specifically 256-299).
The last group (\d{1,5})? is to catch the port number, again you will have to examine this as it will catch any 1 to 5 digit number (hence the {1,5}). The ? makes the group optional, remove it if you want it to have to match against a port number.
As far as doing Regex in C, I haven't had much experience but there should be a way to get all the grouped matches and inspect them. Unfortunately they will be strings so you will have to convert them to integers to examine them.
Are you sure you need regex for this? In my opinion, you do not need regex for this.
Just split numbers into groups which are seperated by non-numeric characters. Then analyze.
What language?
As for actually looking for valid range, take a look at this;
http://www.regular-expressions.info/numericranges.html
I would do this simple regex
((\d|\D)+)*

Combine Regexp?

After collecting user input for various conditions like
Starts with : /(^#)/
Ends with : /(#$)/
Contains : /#/
Doesn't contains
To make single regex if user enter multiple conditions,
I combine them with "|" so if 1 and 2 given it become /(^#)|(#$)/
This method works so far but,
I'm not able to determine correctly, What should be the regex for the 4th condition? And combining regex this way work?
Update: #(user input) won't be same
for two conditions and not all four
conditions always present but they can
be and in future I might need more
conditions like "is exactly" and "is
exactly not" etc. so, I'm more curious
to know this approach will scale ?
Also there may be issues of user input
cleanup so regex escaped properly, but
that is ignored right now.
Will the conditions be ORed or ANDed together?
Starts with: abc
Ends with: xyz
Contains: 123
Doesn't contain: 456
The OR version is fairly simple; as you said, it's mostly a matter of inserting pipes between individual conditions. The regex simply stops looking for a match as soon as one of the alternatives matches.
/^abc|xyz$|123|^(?:(?!456).)*$/
That fourth alternative may look bizarre, but that's how you express "doesn't contain" in a regex. By the way, the order of the alternatives doesn't matter; this is effectively the same regex:
/xyz$|^(?:(?!456).)*$|123|^abc/
The AND version is more complicated. After each individual regex matches, the match position has to be reset to zero so the next regex has access to the whole input. That means all of the conditions have to be expressed as lookaheads (technically, one of them doesn't have to be a lookahead, I think it expresses the intent more clearly this way). A final .*$ consummates the match.
/^(?=^abc)(?=.*xyz$)(?=.*123)(?=^(?:(?!456).)*$).*$/
And then there's the possibility of combined AND and OR conditions--that's where the real fun starts. :D
Doesn't contain #: /(^[^#]*$)/
Combining works if the intended result of combination is that any of them matching results in the whole regexp matching.
If a string must not contain #, every character must be another character than #:
/^[^#]*$/
This will match any string of any length that does not contain #.
Another possible solution would be to invert the boolean result of /#/.
In my experience with regex you really need to focus on what EXACTLY you are trying to match, rather than what NOT to match.
for example
\d{2}
[1-9][0-9]
The first expression will match any 2 digits....and the second will match 1 digit from 1 to 9 and 1 digit - any digit. So if you type 07 the first expression will validate it, but the second one will not.
See this for advanced reference:
http://www.regular-expressions.info/refadv.html
EDITED:
^((?!my string).)*$ Is the regular expression for does not contain "my string".
1 + 2 + 4 conditions: starts|ends, but not in the middle
/^#[^#]*#?$|^#?[^#]*#$/
is almost the same that:
/^#?[^#]*#?$/
but this one matches any string without #, sample 'my name is hal9000'
Combining the regex for the fourth option with any of the others doesn't work within one regex. 4 + 1 would mean either the string starts with # or doesn't contain # at all. You're going to need two separate comparisons to do that.