Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need a regular expression for the elo credit card which should allow only first 6 digits are mentioned below. The total length will be 16 and all 16 should be numbers only. Alphabets are not allow.
Allowed prefixes:
401178, 401179, 431274, 438935, 451416, 457393, 457631, 457632,
504175, 627780, 636297, 636368, 655000, 655001, 651652, 651653,
651654, 650485, 650486, 650487, 650488, 506699 to 506778 and 509000
to 509999
Use an alternation, with a bit of extra work to cover the two numerical ranges you have.
^(?:401178|401179|431274|438935|451416|457393|457631|457632|504175|627780|636297|636368|
655000|655001|651652|651653|651654|650485|650486|650487|650488|506699|5067[0-6][0-9]|
50677[0-8]|509\d{3})\d{10}$
Here is how we handle the two ranges:
506699 to 506778
506699| matches 506699
5067[0-6][0-9]| matches 506700 through and including 506769
50677[0-8] matches 506770 through and including 506778
509000 to 509999
509\d{3} matches 509000 through and including 509999
i.e. 509 followed by any 3 digits
Demo here:
Regex101
You can try this:
^(?:40117[8-9]|431274|438935|451416|457393|45763[1-2]|504175
|627780|636297|636368|65500[0-1]|65165[2-4]|65048[5-8]|506699
|5067[0-6]\d|50677[0-8]|509\d{3})\d{10}$
Demo
Simple Explanation
^ Start of the line
( start of group
?: will not store it in the group
40117[8-9] means 40117 followed by anything between 8 to 9 ( same
applies for similars)
| means OR
5067[0-6]\d means 5067 + a digit between 0 to 6 + a single digit
(any)
\d{10} means it will see if the next 10 characters are digits (after previous valid 6 digits)
$ end of the line
Basically, you need alternation with some range operators to shorten the regex.
The most tricky part is to define the range 506699 to 506778, which can be represented as 506699|5067[06]\d|50677[0-8].
(?x)^(?:
40117[89]|431274|438935|451416|457393|457631|457632|504175
|627780|636297|636368|65500[01]|65165[234]|65048[5-8]
|506699|5067[06]\d|50677[0-8]
|509\d{3}
)\d{10}$
Demo: https://regex101.com/r/BbnHeQ/2
NB: the (?x) is used to allow for whitespace characters in the regex, which simplifies reading for log expressions.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have the following sentence:
b.g The big bag of bits was bugged.
How can I exclude the b.g from it by using a regular expression?
I am sure I need a negative lookahead but I cannot get it right yet.
Something like
^(?!b\.g)
I would do it this way:
[^\S].*
What [^\S] does is basically skip any character until it reaches the first space. then start capturing. No need in this case for negative or positing Lookbehind.
Demo: regex101
If you prefer to do it with positive Lookbehind, you can do it this way
(?<=b\.g).*
Demo: regex101
sed 's/^...//' strips the first 3 characters, "b.g", but I doubt that's what you're really asking. Your ^ anchor appears to be a red herring.
You already have correct escaping for . period, just stick with that:
sed 's/b\.g//'
Python's positive lookbehind ?<= may be what you are trying to find words to express:
>>> m = re.search(r'(?<=b\.g)(.*)', 'b.g The big bag of bits was bugged.')
>>> print(m.group(1))
The big bag of bits was bugged.
In python you could do something like this:
import re
w = 'b.g The big bag of bits was bugged.'
print w
d = re.compile(r'^b.g\s')
a = re.sub(d, '', w)
print a
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Which expression should I use to identify the number of hydrogen atoms in a chemical formula?
For example:
C40H51N11O19 - 51 hydrogens
C2HO - 1 hydrogen
CO2 - no hydrogens (empty)
Any suggestions?
Thanks!
Cheers!
You can start using this regex :
H\d*
H -> match literaly the H caracter
d* -> match 0 to N time a digit
see exemple and try yourself other regex at :
https://regex101.com/r/vdvH8S/2
But regex wont convert for you the result, regex only do lookup.
You need to process your result saying :
H with a number : extract the number
only H : 1
no match : 0
A Regex Expression that will match H with follwowing digits would be:
/H(\d+)/g
The 'H' is a literal charecter match to the H in the given chemical
formula
() declares a capture group, so you cna then grab the captured group without the H in whatever programming language you are using
\d will match any digit along with the + modifier that matches 1 or more
There is no catch all scenarios here, you might be best using something other than a regex.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need to limit an accepted text response to be only in the following format:
1234567.123 AorABorABCD (1or12)
So, 7-digits.3-digits 1-4Letters (1-3-digits)
This is a SKU number, followed by 1-4 letters noting size (S or YM or SMMD) followed by 1-3 digits noting quantity needed.
Any help is appreciated -- thanks!
Something like this?
\d{7}\.\d{3}\s[A-Za-z]{1,4}
Explanation:
\d{7} - matches 7 digits
\. matches the dot (".")
\d{3} - again, matches 3 digits
\s - matches a whitespace
[A-Za-z]{1,4} - matches 1-4 letters from A to Z no matter the case.
Will match:
1234567.123 AABd
1534567.113 AaAA
4586451.773 dasA
1231846.123 A
1234567.123 Ao
1234567.123 Aor
1234567.123 AorA
1234567.123 AorA
1234567.123 AorA
Also, if you do not need all letters from A to Z change the range accordingly.
EDIT: Forgot the slash before the dot, mind the changed content.
You can try it here
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm not really good at regex or I can say that I'm a totally beginner to it. I want to know the regex from the following format:
LB-[0-999] - "LB-" with 0 - 999 digits and no spaces at the beginning, middle, and end.
XX-[0-19999] - Only two Capitalize letters in any combination with "-" and 0-19999 digits and no spaces at the beginning, middle and end.
XXX-[0-19999] - Only three Capitalize letters in any combination with "-" and 0-19999 digits and no spaces at the beginning, middle and end.
I want to get all three patterns but I'm really new to regex. I was planning to use it as html5 input validation and I'm really out of time of studying it.
This is what I tried so far:
^LB-[0-9]$
/LB\-[0-9]{1,3}/
/[A-Z]{2}\-1?[0-9]{1,4}/
/[A-Z]{3}\-1?[0-9]{1,4}/
With [0-9] you make appear only numbers in this set: 0 until 9 and with [A-Z] only capital letter from alphabeta. In {1,3} and {1,4} you make obligated to have at least one letter/number and at most four or three. With ?1 you make optional the present of a number 1 before your number, that will be present only for 10000 number or greater. This are three different er for each one of your possible entries.
Consider the changes prosed by user in his last comment the code will be like this:
/LB\-(00[1-9] | [1-9][0-9] | [1-9][0-9]{2})/
/[A-Z]{2}\-(0000[1-9] | 000[1-9][0-9] | 00[1-9][0-9]{2} | 0[1-9][0-9]{3} | 1[0-9][0-9]{3})/
/[A-Z]{3}\-(0000[1-9] | 000[1-9][0-9] | 00[1-9][0-9]{2} | 0[1-9][0-9]{3} | 1[0-9][0-9]{3})/
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm looking for a way to check if a number is greater than 0 using regex.
For example:
12 would return true
0 would return false.
I don't know how MVC is relevant, but if your ID is an integer, this BRE should do:
^[1-9][0-9]*$
If you want to match real numbers (floats) rather than integers, you need to handle the case above, along with normal decimal numbers (i.e. 2.5 or 3.3̅), cases where your pattern is between 0 and 1 (i.e. 0.25), as well as case where your pattern has a decimal part that is 0. (i.e. 2.0). And while we're at it, we'll add support for leading zeros on integers (i.e. 005):
^(0*[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*)$
Note that this second one is an Extended RE. The same thing can be expressed in Basic RE, but almost everything understands ERE these days. Let's break the expression down into parts that are easier to digest.
^(
The caret matches the null at the beginning of the line, so preceding your regex with a caret anchors it to the beginning of the line. The opening parenthesis is there because of the or-bar, below. More on that later.
0*[1-9][0-9]*(\.[0-9]+)?
This matches any integer or any floating point number above 1. So our 2.0 would be matched, but 0.25 would not. The 0* at the start handles leading zeros, so 005 == 5.
|
The pipe character is an "or-bar" in this context. For purposes of evaluation of this expression, It has higher precedence than everything else, and effectively joins two regular expressions together. Parentheses are used to group multiple expressions separated by or-bars.
And the second part:
0+\.[0-9]*[1-9][0-9]*
This matches any number that starts with one or more 0 characters (replace + with * to match zero or more zeros, i.e. .25), followed by a period, followed by a string of digits that includes at least one that is not a 0. So this matches everything above 0 and below 1.
)$
And finally, we close the parentheses and anchor the regex to the end of the line with the dollar sign, just as the caret anchors to the beginning of the line.
Of course, if you let your programming language evaluate something numerically rather than try to match it against a regular expression, you'll save headaches and CPU.
What about this: ^[1-9][0-9]*$
Another solution for integer:
^[1-9]\d*$
\d equivalent to [0-9]
Code:
^([0-9]*[1-9][0-9]*(\.[0-9]+)?|[0]+\.[0-9]*[1-9][0-9]*)$
Example: http://regexr.com/3anf5
Reference: https://social.msdn.microsoft.com/Forums/en-US/17089c0f-f9cb-437a-9667-ba8329681624/regular-expression-number-greater-than-0?forum=regexp
I think the best solution is to add the + sign between the two brackets of regex expression:
^[1-9]+[0-9]*$
If you only want non-negative integers, try:
^\d+$
I Tried this one and it worked for me for all decimal/integer numbers greater than zero
Allows white space: ^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$
No white space: ^(?=.*[1-9])\d*(?:\.\d{1,2})?$
Reference: Regex greater than zero with 2 decimal places
there you go:
MatchCollection myMatches = Regex.Matches(yourstring, #"[1-9][0-9]*");
on submit:
if(myMatches.Count > 0)
{
//do whatever you want
}
You can use the below expression:
(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\.?\d*$)
Valid entries: 1 1. 1.1 1.0 all positive real numbers
Invalid entries: all negative real numbers and 0 and 0.0
Simplified only for 2 decimal places.
^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$
Ref: https://www.regextester.com/94470
The simple answer is: ^[1-9][0-9]*$
I think this would perfectly work :
([1-9][0-9]*(\.[0-9]*[1-9])?|0\.[0-9]*[1-9])
Valid:
1
1.2
1.02
0.1
0.02
Not valid :
0
01
01.2
1.10
[1-9]\.\d{1,2}|0\.((0?[1-9])|([1-9]0?)){1,2}\b
Very simple answer to this use this: \d*