regex - any number of digits + digit or [a-z]
I am trying to write a regular expresion that checks if a string starts with a number of digits (at least one), and then immediately ends with a single letter or a digit.
So:
29c
is fine
29
is fine
2425315651252fsaw
fails
24241jl.421c
fails
c
fails
The regex I have so far is (^d+)([a-z]{1}|d)
which passes the 29
, 20c
, but also passes stuff like 29cdsd
.
What am I doing wrong?
regex
add a comment |
I am trying to write a regular expresion that checks if a string starts with a number of digits (at least one), and then immediately ends with a single letter or a digit.
So:
29c
is fine
29
is fine
2425315651252fsaw
fails
24241jl.421c
fails
c
fails
The regex I have so far is (^d+)([a-z]{1}|d)
which passes the 29
, 20c
, but also passes stuff like 29cdsd
.
What am I doing wrong?
regex
Please let know ifa
and1
are valid inputs.
– Wiktor Stribiżew
Nov 23 '18 at 13:05
add a comment |
I am trying to write a regular expresion that checks if a string starts with a number of digits (at least one), and then immediately ends with a single letter or a digit.
So:
29c
is fine
29
is fine
2425315651252fsaw
fails
24241jl.421c
fails
c
fails
The regex I have so far is (^d+)([a-z]{1}|d)
which passes the 29
, 20c
, but also passes stuff like 29cdsd
.
What am I doing wrong?
regex
I am trying to write a regular expresion that checks if a string starts with a number of digits (at least one), and then immediately ends with a single letter or a digit.
So:
29c
is fine
29
is fine
2425315651252fsaw
fails
24241jl.421c
fails
c
fails
The regex I have so far is (^d+)([a-z]{1}|d)
which passes the 29
, 20c
, but also passes stuff like 29cdsd
.
What am I doing wrong?
regex
regex
edited Nov 23 '18 at 13:15
Miha Šušteršič
asked Nov 23 '18 at 12:55
Miha ŠušteršičMiha Šušteršič
3,711104479
3,711104479
Please let know ifa
and1
are valid inputs.
– Wiktor Stribiżew
Nov 23 '18 at 13:05
add a comment |
Please let know ifa
and1
are valid inputs.
– Wiktor Stribiżew
Nov 23 '18 at 13:05
Please let know if
a
and 1
are valid inputs.– Wiktor Stribiżew
Nov 23 '18 at 13:05
Please let know if
a
and 1
are valid inputs.– Wiktor Stribiżew
Nov 23 '18 at 13:05
add a comment |
5 Answers
5
active
oldest
votes
Your (^d+)([a-z]{1}|d)
passes 29cdsd
because it matches 1 or more digits at the start of the string followed with 1 letter or 1 digit, and allows anything right after.
Use
^[0-9]+[a-z0-9]?$
See regex demo
Details
^
- start of string
[0-9]+
- any 1 or more digits
[a-z0-9]?
- 1 or 0 lowercase ASCII letters or digits
$
- end of string.
1
Wouldn't this allow the string to be a single character a-z?
– ohaal
Nov 23 '18 at 12:59
@ohaal Yes, these are the requirements. a string starts with any number of digits, and then immediately ends with a single letter or a digit. Any means 0 or more.
– Wiktor Stribiżew
Nov 23 '18 at 12:59
Ah, yes, he says "any number of digits", so I guess that means 0 or more. It is me who has misunderstood the question.
– ohaal
Nov 23 '18 at 13:00
@ohaal Well, we need confirmation from OP. Your^d+[a-z]?$
solution might be actually what OP wants in the end if there is a requirement to disallow just one letter strings. I prefer[0-9]
here because OP did not specify the regex flavor. Not all flavors supportd
.
– Wiktor Stribiżew
Nov 23 '18 at 13:03
Ugh sry, yes @ohaal is correct - the string cannot start with a character. Edited the question so it is more clear.
– Miha Šušteršič
Nov 23 '18 at 13:14
|
show 1 more comment
This should follow your rules exactly.
^d+[a-z]?$
add a comment |
if "any number of digits" might be zero ^d*w$
add a comment |
You could add an anchor $
to assert the end of the line and you can omit the {1}
part:
^(d+)([a-z]|d)$
In your regex you are matching a minimum of 2 characters .If you don't need the capturing groups, this could also be written as:
^d+[a-zd]$
Regex demo
That would match:
^
Assert the start of the string
d+
Match 1+ digits
[a-zd]
A character class which matches a-z or a digit
$
Assert the end of the string
add a comment |
^
- start of string
d*
- any amount of digits, 0 or more
[a-zA-z]
- a lowercase and uppercase ASCII letters.
$
- end of string
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447133%2fregex-any-number-of-digits-digit-or-a-z%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your (^d+)([a-z]{1}|d)
passes 29cdsd
because it matches 1 or more digits at the start of the string followed with 1 letter or 1 digit, and allows anything right after.
Use
^[0-9]+[a-z0-9]?$
See regex demo
Details
^
- start of string
[0-9]+
- any 1 or more digits
[a-z0-9]?
- 1 or 0 lowercase ASCII letters or digits
$
- end of string.
1
Wouldn't this allow the string to be a single character a-z?
– ohaal
Nov 23 '18 at 12:59
@ohaal Yes, these are the requirements. a string starts with any number of digits, and then immediately ends with a single letter or a digit. Any means 0 or more.
– Wiktor Stribiżew
Nov 23 '18 at 12:59
Ah, yes, he says "any number of digits", so I guess that means 0 or more. It is me who has misunderstood the question.
– ohaal
Nov 23 '18 at 13:00
@ohaal Well, we need confirmation from OP. Your^d+[a-z]?$
solution might be actually what OP wants in the end if there is a requirement to disallow just one letter strings. I prefer[0-9]
here because OP did not specify the regex flavor. Not all flavors supportd
.
– Wiktor Stribiżew
Nov 23 '18 at 13:03
Ugh sry, yes @ohaal is correct - the string cannot start with a character. Edited the question so it is more clear.
– Miha Šušteršič
Nov 23 '18 at 13:14
|
show 1 more comment
Your (^d+)([a-z]{1}|d)
passes 29cdsd
because it matches 1 or more digits at the start of the string followed with 1 letter or 1 digit, and allows anything right after.
Use
^[0-9]+[a-z0-9]?$
See regex demo
Details
^
- start of string
[0-9]+
- any 1 or more digits
[a-z0-9]?
- 1 or 0 lowercase ASCII letters or digits
$
- end of string.
1
Wouldn't this allow the string to be a single character a-z?
– ohaal
Nov 23 '18 at 12:59
@ohaal Yes, these are the requirements. a string starts with any number of digits, and then immediately ends with a single letter or a digit. Any means 0 or more.
– Wiktor Stribiżew
Nov 23 '18 at 12:59
Ah, yes, he says "any number of digits", so I guess that means 0 or more. It is me who has misunderstood the question.
– ohaal
Nov 23 '18 at 13:00
@ohaal Well, we need confirmation from OP. Your^d+[a-z]?$
solution might be actually what OP wants in the end if there is a requirement to disallow just one letter strings. I prefer[0-9]
here because OP did not specify the regex flavor. Not all flavors supportd
.
– Wiktor Stribiżew
Nov 23 '18 at 13:03
Ugh sry, yes @ohaal is correct - the string cannot start with a character. Edited the question so it is more clear.
– Miha Šušteršič
Nov 23 '18 at 13:14
|
show 1 more comment
Your (^d+)([a-z]{1}|d)
passes 29cdsd
because it matches 1 or more digits at the start of the string followed with 1 letter or 1 digit, and allows anything right after.
Use
^[0-9]+[a-z0-9]?$
See regex demo
Details
^
- start of string
[0-9]+
- any 1 or more digits
[a-z0-9]?
- 1 or 0 lowercase ASCII letters or digits
$
- end of string.
Your (^d+)([a-z]{1}|d)
passes 29cdsd
because it matches 1 or more digits at the start of the string followed with 1 letter or 1 digit, and allows anything right after.
Use
^[0-9]+[a-z0-9]?$
See regex demo
Details
^
- start of string
[0-9]+
- any 1 or more digits
[a-z0-9]?
- 1 or 0 lowercase ASCII letters or digits
$
- end of string.
edited Nov 23 '18 at 13:15
answered Nov 23 '18 at 12:56
Wiktor StribiżewWiktor Stribiżew
310k16131206
310k16131206
1
Wouldn't this allow the string to be a single character a-z?
– ohaal
Nov 23 '18 at 12:59
@ohaal Yes, these are the requirements. a string starts with any number of digits, and then immediately ends with a single letter or a digit. Any means 0 or more.
– Wiktor Stribiżew
Nov 23 '18 at 12:59
Ah, yes, he says "any number of digits", so I guess that means 0 or more. It is me who has misunderstood the question.
– ohaal
Nov 23 '18 at 13:00
@ohaal Well, we need confirmation from OP. Your^d+[a-z]?$
solution might be actually what OP wants in the end if there is a requirement to disallow just one letter strings. I prefer[0-9]
here because OP did not specify the regex flavor. Not all flavors supportd
.
– Wiktor Stribiżew
Nov 23 '18 at 13:03
Ugh sry, yes @ohaal is correct - the string cannot start with a character. Edited the question so it is more clear.
– Miha Šušteršič
Nov 23 '18 at 13:14
|
show 1 more comment
1
Wouldn't this allow the string to be a single character a-z?
– ohaal
Nov 23 '18 at 12:59
@ohaal Yes, these are the requirements. a string starts with any number of digits, and then immediately ends with a single letter or a digit. Any means 0 or more.
– Wiktor Stribiżew
Nov 23 '18 at 12:59
Ah, yes, he says "any number of digits", so I guess that means 0 or more. It is me who has misunderstood the question.
– ohaal
Nov 23 '18 at 13:00
@ohaal Well, we need confirmation from OP. Your^d+[a-z]?$
solution might be actually what OP wants in the end if there is a requirement to disallow just one letter strings. I prefer[0-9]
here because OP did not specify the regex flavor. Not all flavors supportd
.
– Wiktor Stribiżew
Nov 23 '18 at 13:03
Ugh sry, yes @ohaal is correct - the string cannot start with a character. Edited the question so it is more clear.
– Miha Šušteršič
Nov 23 '18 at 13:14
1
1
Wouldn't this allow the string to be a single character a-z?
– ohaal
Nov 23 '18 at 12:59
Wouldn't this allow the string to be a single character a-z?
– ohaal
Nov 23 '18 at 12:59
@ohaal Yes, these are the requirements. a string starts with any number of digits, and then immediately ends with a single letter or a digit. Any means 0 or more.
– Wiktor Stribiżew
Nov 23 '18 at 12:59
@ohaal Yes, these are the requirements. a string starts with any number of digits, and then immediately ends with a single letter or a digit. Any means 0 or more.
– Wiktor Stribiżew
Nov 23 '18 at 12:59
Ah, yes, he says "any number of digits", so I guess that means 0 or more. It is me who has misunderstood the question.
– ohaal
Nov 23 '18 at 13:00
Ah, yes, he says "any number of digits", so I guess that means 0 or more. It is me who has misunderstood the question.
– ohaal
Nov 23 '18 at 13:00
@ohaal Well, we need confirmation from OP. Your
^d+[a-z]?$
solution might be actually what OP wants in the end if there is a requirement to disallow just one letter strings. I prefer [0-9]
here because OP did not specify the regex flavor. Not all flavors support d
.– Wiktor Stribiżew
Nov 23 '18 at 13:03
@ohaal Well, we need confirmation from OP. Your
^d+[a-z]?$
solution might be actually what OP wants in the end if there is a requirement to disallow just one letter strings. I prefer [0-9]
here because OP did not specify the regex flavor. Not all flavors support d
.– Wiktor Stribiżew
Nov 23 '18 at 13:03
Ugh sry, yes @ohaal is correct - the string cannot start with a character. Edited the question so it is more clear.
– Miha Šušteršič
Nov 23 '18 at 13:14
Ugh sry, yes @ohaal is correct - the string cannot start with a character. Edited the question so it is more clear.
– Miha Šušteršič
Nov 23 '18 at 13:14
|
show 1 more comment
This should follow your rules exactly.
^d+[a-z]?$
add a comment |
This should follow your rules exactly.
^d+[a-z]?$
add a comment |
This should follow your rules exactly.
^d+[a-z]?$
This should follow your rules exactly.
^d+[a-z]?$
answered Nov 23 '18 at 12:57
ohaalohaal
4,69222646
4,69222646
add a comment |
add a comment |
if "any number of digits" might be zero ^d*w$
add a comment |
if "any number of digits" might be zero ^d*w$
add a comment |
if "any number of digits" might be zero ^d*w$
if "any number of digits" might be zero ^d*w$
answered Nov 23 '18 at 13:15
ekvalizerekvalizer
214
214
add a comment |
add a comment |
You could add an anchor $
to assert the end of the line and you can omit the {1}
part:
^(d+)([a-z]|d)$
In your regex you are matching a minimum of 2 characters .If you don't need the capturing groups, this could also be written as:
^d+[a-zd]$
Regex demo
That would match:
^
Assert the start of the string
d+
Match 1+ digits
[a-zd]
A character class which matches a-z or a digit
$
Assert the end of the string
add a comment |
You could add an anchor $
to assert the end of the line and you can omit the {1}
part:
^(d+)([a-z]|d)$
In your regex you are matching a minimum of 2 characters .If you don't need the capturing groups, this could also be written as:
^d+[a-zd]$
Regex demo
That would match:
^
Assert the start of the string
d+
Match 1+ digits
[a-zd]
A character class which matches a-z or a digit
$
Assert the end of the string
add a comment |
You could add an anchor $
to assert the end of the line and you can omit the {1}
part:
^(d+)([a-z]|d)$
In your regex you are matching a minimum of 2 characters .If you don't need the capturing groups, this could also be written as:
^d+[a-zd]$
Regex demo
That would match:
^
Assert the start of the string
d+
Match 1+ digits
[a-zd]
A character class which matches a-z or a digit
$
Assert the end of the string
You could add an anchor $
to assert the end of the line and you can omit the {1}
part:
^(d+)([a-z]|d)$
In your regex you are matching a minimum of 2 characters .If you don't need the capturing groups, this could also be written as:
^d+[a-zd]$
Regex demo
That would match:
^
Assert the start of the string
d+
Match 1+ digits
[a-zd]
A character class which matches a-z or a digit
$
Assert the end of the string
edited Nov 23 '18 at 13:27
answered Nov 23 '18 at 13:13
The fourth birdThe fourth bird
21.1k71326
21.1k71326
add a comment |
add a comment |
^
- start of string
d*
- any amount of digits, 0 or more
[a-zA-z]
- a lowercase and uppercase ASCII letters.
$
- end of string
add a comment |
^
- start of string
d*
- any amount of digits, 0 or more
[a-zA-z]
- a lowercase and uppercase ASCII letters.
$
- end of string
add a comment |
^
- start of string
d*
- any amount of digits, 0 or more
[a-zA-z]
- a lowercase and uppercase ASCII letters.
$
- end of string
^
- start of string
d*
- any amount of digits, 0 or more
[a-zA-z]
- a lowercase and uppercase ASCII letters.
$
- end of string
edited Nov 23 '18 at 13:21
answered Nov 23 '18 at 13:13
GK DevloperGK Devloper
12
12
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447133%2fregex-any-number-of-digits-digit-or-a-z%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please let know if
a
and1
are valid inputs.– Wiktor Stribiżew
Nov 23 '18 at 13:05