I am looking for a regex expression that will accept the following: The capital letter A followed by any number of digits. This might also be a decimal number. All of these are valid: A1, A500, A543.987
This is NOT OK to accept: Apple, AE100
Currently I have [A]\w.[0-9]* but it accepts App and AE100.
You may use the following regex if the entire string should match:
^A[0-9]+(?:\.[0-9]+)?$
Or, to match these strings as whole words:
\bA[0-9]+(?:\.[0-9]+)?\b
See the regex demo.
Details
^ - start of string / \b - a word boundary
A - an A
[0-9]+ - 1+ digits
(?:\.[0-9]+)? - an optional sequence of . and then 1+ digits
$ - end of string / \b - a word boundary.
I would suggest "A\d+(\.\d+)?". \d represents all digits, the + is one or more characters and the (\.\d+)? is a . followed by one or more digits. But the ? specifies it's optional.
Related
Is it possible to match just the numbers from a predefined set of characters looking like:
FACTURA SBG113151
What I want: 113151
What I'm trying: FACTURA[ ]*([a-z0-9A-Z]+)
What I get: SBG113151
Any help is much appreciated! Thanks.
You can use
FACTURA *[a-zA-Z]*([0-9]+)
FACTURA[^0-9]*([0-9]+)
See the regex demo. The point is to match and consume the chars before the digit sequence you need to extract.
Details:
FACTURA - a word
* - zero or more spaces
[a-zA-Z]* - zero or more letters
[^0-9]* - in the other regex, matches zero or more chars other than digits
([0-9]+) - Group 1: one or more digits
I need to match only section numbers in text with a dot at the end. For example, having a string:
'A.8. 8.4.2.4.1.2. 9.1. 9. 10.0.1.1. 9 0.1 100. 100.5. A.500'
What I want to match: [A.8., 8.4.2.4.1.2., 9.1., 9., 10.0.1.1., 100., 100.5.]
What I have matched: [A.8., 8.4.2.4.1.2., 9.1., 10.0.1.1., 100.5.]
My regex is (?:\d+|A)\.[\d+\.]*\.
Those numbers, without the dot at the end are not matched, which is correct. However, singular numbers with a dot should be matched but are not (such as '9.' and '100.')
How can I update my regex to make it work?
You may use
\b(?:\d+|A)(?:\.\d+)*\.\B
See the regex demo.
If the matches are whitespace-separated you can also use (granted the regex engine supports lookbehinds):
(?<!\S)(?:\d+|A)(?:\.\d+)*\.(?!\S)
See the regex demo.
Details:
\b - a word boundary (start of string or a non-word char should immediately precede the next digit or A)
(?:\d+|A) - one or more digits, or an A
(?:\.\d+)* - zero or more repetitions of a dot and then one or more digits
\. - a dot
\B - a position other than a word boundary (end of string or a non-word char should follow immediately)
The (?<!\S) / (?!\S) require a whitespace or start/end of string positions on both ends of the match.
I have to match a string that is 3-6 characters long, contains at least one letter, but can have letters, numbers and only 1 "-".
The "-" must not be at the start or at the beginning.
Match:
string
str-ng
st-ng
s1-1g
st-1g
Do not match:
strings
-string
string-
st--ng
s-tn-g
1111
st
The closest I've gotten is this:
^((?!-.*-)[0-9A-Z]{3,6})$
But this divides the regex match with - So it matches s-tri but not st-ri because there aren't 3 chars at each end
Maybe you can use:
^(?=.*[a-z])(?!-|.*-$|.*-.*-)[a-z\d-]{3,6}$
See the online demo
^ - Start string anchor.
(?=.*[a-z]) - Positive lookahead to make sure there is at least one letter.
(?!-|.*-$|.*-.*-) - Negative lookahead to prevent a hyphen at the beginning or at the end or multiple.
[a-z\d-]{3,6} - Three to six times a character from the give class.
$ - End string anchor.
Note that I used the case-insensitive flag.
You can use
^(?=.{3,6}$)(?=[^a-zA-Z]*[A-Za-z])[0-9a-zA-Z]+(?:-[0-9a-zA-Z]+)?$
See the regex demo. Details:
^ - start of string
(?=.{3,6}$) - string must contain three to six chars other than line break chars
(?=[^a-zA-Z]*[A-Za-z]) - there must be at least one ASCII letter in the string
[0-9a-zA-Z]+ - one or more alphanumeric ASCII chars
(?:-[0-9a-zA-Z]+)? - an optional sequence of - and then one or more alphanumeric ASCII chars
$ - end of string.
Looking at the pattern that you tried, you meant to exclude the match when there are 2 hyphens present using the negative lookahead.
Also this part [0-9A-Z]{3,6} does not match a hyphen.
Reading
The "-" must not be at the start or at the beginning.
You might do that using
^(?![^\n-]*-[^\n-]*-)(?=[^a-zA-Z\n]*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9-]{2,5}$
Regex demo
If you meant also no - at the end:
^(?![^\n-]*-[^\n-]*-)(?=[^a-zA-Z\n]*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9-]{1,4}[a-zA-Z0-9]$
Explanation
^ Start of string
(?![^\n-]*-[^\n-]*-) Assert not 2 times -
(?=[^a-zA-Z\n]*[a-zA-Z]) Assert a char a-zA-Z
[a-zA-Z0-9] Match One of the listed without -
[a-zA-Z0-9-]{1,4} Repeat 1-4 times any of the listed including -
[a-zA-Z0-9] Match One of the listed without -
$ End of string
Regex demo
To match a dash-less checksum I can do something like:
\b[0-9a-z]{32}\b
However, I'm seeing some checksums that also have dashes, such as:
d3bd55bf-062f-473b-9417-935f62c4c98a
While this is probably a fixed size, 8, then 4, then 4, then 4, then 12, I was wondering if I could do a regex where the number of non-dash digits adds up to 32. I think the answer is no, but hopefully some regex wizard can come up with something.
Here is a starting point for some sample inputs: https://regex101.com/r/K0IMKe/1.
You can use
\b[0-9a-z](?:-?[0-9a-z]){31}\b
See the regex demo.
It matches
\b - a word boundary
[0-9a-z] - a digit or a lowercase ASCII letter
(?:-?[0-9a-z]){31} - thirty-one repetitions of an optional - followed with a single digit or a lowercase ASCII letter
\b - a word boundary.
If you do not mind having a trailing - if there is a word char after it, at the end of a match, you may also use
\b(?:[0-9a-z]-?){32}\b
See this regex demo. Here, (?:[0-9a-z]-?){32} will match thirty-two repetitions of a digit or lowercase ASCII letter followed with an optional hyphen.
If there can be multiple dashes, you can assert 32 to 36 chars using a positive lookahead.
^(?=[a-z0-9-]{32,36}$)[a-z0-9]+(?:-[a-z0-9]+)*$
^ Start of string
(?=[a-z0-9-]{32,36}$) Positive lookahead, assert what is at the right is 32 - 36 repetitions of the listed characters
[a-z0-9]+ Match 1+ times any of the listed
(?: Non capture group
-[a-z0-9]+ Match a - followed by 1+ times any of the listed (the string can not end with a hyphen)
)* Close the group and match 0+ times to also match the string without dashes
$ End of string
Regex demo
If you want to limit the amount of dashes to 0 -4 times, you can change the quantifier * to {0,4}+
^(?=[a-z0-9-]{32,36}$)[a-z0-9]+(?:-[a-z0-9]+){0,4}+$
Regex demo
I would like to match String that contains letters and numbers WITHOUT space
I tried ^[a-zA-Z+d*]*$ but it matches String that have only letters
This is what it should do :
Nope
Nope 2
MatchPlease123
If you want to try in live:
http://rubular.com/r/pFMkk9ATc0
Thank you
You may use
/^(?=[^a-zA-Z]*[a-zA-Z])(?=[^0-9]*[0-9])[a-zA-Z0-9]*$/
See the regex demo (a bit modified since the input is a multiline string).
Details:
^ - start of a string
(?=[^a-zA-Z]*[a-zA-Z]) - a positive lookahead requiring that there must be at least one ASCII letter after any 0+ chars other than ASCII letters
(?=[^0-9]*[0-9]) - a positive lookahead requiring that there must be at least one ASCII digit after any 0+ chars other than ASCII digits
[a-zA-Z0-9]* - 0+ ASCII letters or digits
$ - end of string.
I think this simplest one will also be helpful
Regex demo
Regex: ^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z\d]+$
1. ^ start of string.
2. (?=.*\d) positive look ahead for digit.
3. (?=.*[a-zA-Z]) positive look alphabets.
4. [a-zA-Z\d]+ match all digits A-Z and a-z
5. $ end of string.