Simplify Regular Expression with variable amount of alphanumeric character separated by a dash
up vote
0
down vote
favorite
I would like to validate a specific string to be correct. A correct string looks like the following:
ab1-peter-application
The rules for the string are <1-4 alphanumeric characters>-<1-30 alphanumeric characters>-<1-30 alphanumeric characters>
I just build the regex as following which I guess would work:
[a-zA-Z0-9]{1,4}[-]{1}[a-zA-Z0-9]{1,30}[-]{1}[a-zA-Z0-9]{1,30}
As every segment seems to be the same and the occurrence of the alphanumeric characters vary I feel like it can be shortened. Is there any chance to shorten/simplify this regex?
regex
add a comment |
up vote
0
down vote
favorite
I would like to validate a specific string to be correct. A correct string looks like the following:
ab1-peter-application
The rules for the string are <1-4 alphanumeric characters>-<1-30 alphanumeric characters>-<1-30 alphanumeric characters>
I just build the regex as following which I guess would work:
[a-zA-Z0-9]{1,4}[-]{1}[a-zA-Z0-9]{1,30}[-]{1}[a-zA-Z0-9]{1,30}
As every segment seems to be the same and the occurrence of the alphanumeric characters vary I feel like it can be shortened. Is there any chance to shorten/simplify this regex?
regex
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I would like to validate a specific string to be correct. A correct string looks like the following:
ab1-peter-application
The rules for the string are <1-4 alphanumeric characters>-<1-30 alphanumeric characters>-<1-30 alphanumeric characters>
I just build the regex as following which I guess would work:
[a-zA-Z0-9]{1,4}[-]{1}[a-zA-Z0-9]{1,30}[-]{1}[a-zA-Z0-9]{1,30}
As every segment seems to be the same and the occurrence of the alphanumeric characters vary I feel like it can be shortened. Is there any chance to shorten/simplify this regex?
regex
I would like to validate a specific string to be correct. A correct string looks like the following:
ab1-peter-application
The rules for the string are <1-4 alphanumeric characters>-<1-30 alphanumeric characters>-<1-30 alphanumeric characters>
I just build the regex as following which I guess would work:
[a-zA-Z0-9]{1,4}[-]{1}[a-zA-Z0-9]{1,30}[-]{1}[a-zA-Z0-9]{1,30}
As every segment seems to be the same and the occurrence of the alphanumeric characters vary I feel like it can be shortened. Is there any chance to shorten/simplify this regex?
regex
regex
asked Nov 12 at 8:03
monti
136214
136214
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
You can combine the final two parts:
-<1-30 alphanumeric characters>-<1-30 alphanumeric characters>
into a single group, repeated twice. Additionally, a single character in a character set is superfluous - you can remove the character set entirely, in that case, to make the regex more concise. The same is true for {1}
(meaningless quantifier):
[a-zA-Z0-9]{1,4}(?:-[a-zA-Z0-9]{1,30}){2}
Additionally, if possible in your environment, use the case-insensitive flag (so you don't have to repeat a-zA-Z
) and also use d
to represent digits instead of 0-9
:
[a-zd]{1,4}(?:-[a-zd]{1,30}){2}
add a comment |
up vote
-1
down vote
[a-zA-Z0-9]{4}(-[a-zA-Z0-9]{30}){2}$ - simple Regex to match exact length required string.
^[a-zA-Z0-9]{1,4}(-[a-zA-Z0-9]{1,30}){2}$ - simple Regex to match variable length required
Hope this will help
lol.. Thats what I had mentioned in my post that it is to match exact string. To match variable string user can easily add the minimum limit to RegEx
– Ashish Sapkale
Nov 12 at 13:50
Answer edited...
– Ashish Sapkale
Nov 12 at 13:54
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',
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%2f53257975%2fsimplify-regular-expression-with-variable-amount-of-alphanumeric-character-separ%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can combine the final two parts:
-<1-30 alphanumeric characters>-<1-30 alphanumeric characters>
into a single group, repeated twice. Additionally, a single character in a character set is superfluous - you can remove the character set entirely, in that case, to make the regex more concise. The same is true for {1}
(meaningless quantifier):
[a-zA-Z0-9]{1,4}(?:-[a-zA-Z0-9]{1,30}){2}
Additionally, if possible in your environment, use the case-insensitive flag (so you don't have to repeat a-zA-Z
) and also use d
to represent digits instead of 0-9
:
[a-zd]{1,4}(?:-[a-zd]{1,30}){2}
add a comment |
up vote
2
down vote
accepted
You can combine the final two parts:
-<1-30 alphanumeric characters>-<1-30 alphanumeric characters>
into a single group, repeated twice. Additionally, a single character in a character set is superfluous - you can remove the character set entirely, in that case, to make the regex more concise. The same is true for {1}
(meaningless quantifier):
[a-zA-Z0-9]{1,4}(?:-[a-zA-Z0-9]{1,30}){2}
Additionally, if possible in your environment, use the case-insensitive flag (so you don't have to repeat a-zA-Z
) and also use d
to represent digits instead of 0-9
:
[a-zd]{1,4}(?:-[a-zd]{1,30}){2}
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can combine the final two parts:
-<1-30 alphanumeric characters>-<1-30 alphanumeric characters>
into a single group, repeated twice. Additionally, a single character in a character set is superfluous - you can remove the character set entirely, in that case, to make the regex more concise. The same is true for {1}
(meaningless quantifier):
[a-zA-Z0-9]{1,4}(?:-[a-zA-Z0-9]{1,30}){2}
Additionally, if possible in your environment, use the case-insensitive flag (so you don't have to repeat a-zA-Z
) and also use d
to represent digits instead of 0-9
:
[a-zd]{1,4}(?:-[a-zd]{1,30}){2}
You can combine the final two parts:
-<1-30 alphanumeric characters>-<1-30 alphanumeric characters>
into a single group, repeated twice. Additionally, a single character in a character set is superfluous - you can remove the character set entirely, in that case, to make the regex more concise. The same is true for {1}
(meaningless quantifier):
[a-zA-Z0-9]{1,4}(?:-[a-zA-Z0-9]{1,30}){2}
Additionally, if possible in your environment, use the case-insensitive flag (so you don't have to repeat a-zA-Z
) and also use d
to represent digits instead of 0-9
:
[a-zd]{1,4}(?:-[a-zd]{1,30}){2}
answered Nov 12 at 8:06
CertainPerformance
72.5k143453
72.5k143453
add a comment |
add a comment |
up vote
-1
down vote
[a-zA-Z0-9]{4}(-[a-zA-Z0-9]{30}){2}$ - simple Regex to match exact length required string.
^[a-zA-Z0-9]{1,4}(-[a-zA-Z0-9]{1,30}){2}$ - simple Regex to match variable length required
Hope this will help
lol.. Thats what I had mentioned in my post that it is to match exact string. To match variable string user can easily add the minimum limit to RegEx
– Ashish Sapkale
Nov 12 at 13:50
Answer edited...
– Ashish Sapkale
Nov 12 at 13:54
add a comment |
up vote
-1
down vote
[a-zA-Z0-9]{4}(-[a-zA-Z0-9]{30}){2}$ - simple Regex to match exact length required string.
^[a-zA-Z0-9]{1,4}(-[a-zA-Z0-9]{1,30}){2}$ - simple Regex to match variable length required
Hope this will help
lol.. Thats what I had mentioned in my post that it is to match exact string. To match variable string user can easily add the minimum limit to RegEx
– Ashish Sapkale
Nov 12 at 13:50
Answer edited...
– Ashish Sapkale
Nov 12 at 13:54
add a comment |
up vote
-1
down vote
up vote
-1
down vote
[a-zA-Z0-9]{4}(-[a-zA-Z0-9]{30}){2}$ - simple Regex to match exact length required string.
^[a-zA-Z0-9]{1,4}(-[a-zA-Z0-9]{1,30}){2}$ - simple Regex to match variable length required
Hope this will help
[a-zA-Z0-9]{4}(-[a-zA-Z0-9]{30}){2}$ - simple Regex to match exact length required string.
^[a-zA-Z0-9]{1,4}(-[a-zA-Z0-9]{1,30}){2}$ - simple Regex to match variable length required
Hope this will help
edited Nov 12 at 14:00
answered Nov 12 at 9:33
Ashish Sapkale
454212
454212
lol.. Thats what I had mentioned in my post that it is to match exact string. To match variable string user can easily add the minimum limit to RegEx
– Ashish Sapkale
Nov 12 at 13:50
Answer edited...
– Ashish Sapkale
Nov 12 at 13:54
add a comment |
lol.. Thats what I had mentioned in my post that it is to match exact string. To match variable string user can easily add the minimum limit to RegEx
– Ashish Sapkale
Nov 12 at 13:50
Answer edited...
– Ashish Sapkale
Nov 12 at 13:54
lol.. Thats what I had mentioned in my post that it is to match exact string. To match variable string user can easily add the minimum limit to RegEx
– Ashish Sapkale
Nov 12 at 13:50
lol.. Thats what I had mentioned in my post that it is to match exact string. To match variable string user can easily add the minimum limit to RegEx
– Ashish Sapkale
Nov 12 at 13:50
Answer edited...
– Ashish Sapkale
Nov 12 at 13:54
Answer edited...
– Ashish Sapkale
Nov 12 at 13:54
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53257975%2fsimplify-regular-expression-with-variable-amount-of-alphanumeric-character-separ%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