Regexp struggling
I am trying to match a string (length =4) with lower case letters and digits. That could be 4 digits but not 4 letters. For example I want to match:
d4rt
df5h
34d6
4567
But not 'erty'.
I get that pattern ([a-z]+|[0-9]+){4}
but that keeps me the 4 letters case.
regex
add a comment |
I am trying to match a string (length =4) with lower case letters and digits. That could be 4 digits but not 4 letters. For example I want to match:
d4rt
df5h
34d6
4567
But not 'erty'.
I get that pattern ([a-z]+|[0-9]+){4}
but that keeps me the 4 letters case.
regex
1
what is the language?
– Ulugbek Umirov
Nov 22 '18 at 18:06
add a comment |
I am trying to match a string (length =4) with lower case letters and digits. That could be 4 digits but not 4 letters. For example I want to match:
d4rt
df5h
34d6
4567
But not 'erty'.
I get that pattern ([a-z]+|[0-9]+){4}
but that keeps me the 4 letters case.
regex
I am trying to match a string (length =4) with lower case letters and digits. That could be 4 digits but not 4 letters. For example I want to match:
d4rt
df5h
34d6
4567
But not 'erty'.
I get that pattern ([a-z]+|[0-9]+){4}
but that keeps me the 4 letters case.
regex
regex
edited Nov 22 '18 at 17:57
Foo
1
1
asked Nov 22 '18 at 17:55
arnauddarnaudd
61
61
1
what is the language?
– Ulugbek Umirov
Nov 22 '18 at 18:06
add a comment |
1
what is the language?
– Ulugbek Umirov
Nov 22 '18 at 18:06
1
1
what is the language?
– Ulugbek Umirov
Nov 22 '18 at 18:06
what is the language?
– Ulugbek Umirov
Nov 22 '18 at 18:06
add a comment |
3 Answers
3
active
oldest
votes
Your regex ([a-z]+|[0-9]+){4}
uses an alternation which will either match 1+ lowercase characters or 1+ digits in a capturing group and repeat that 4 times. That would also match 4 letters.
If lookarounds are supported, you could use a negative lookahead to assert that what follows are not 4 lowercase characters.
To match a string with length of 4, you could use anchors to assert the start ^
and the end $
of the string.
^(?![a-z]{4})[a-z0-9]{4}$
Regex demo
add a comment |
Your expression is matching four {4}
of whatever either any number greater than 1 of lower case letters [a-z]
or any number greater than one of digits. Therefore, your code is actually matching more than 4 of letters or digits too.
Your problem can be solved with lookaheads.
(?=[a-z]{0,3}[0-9])[a-z0-9]{4}
(?=[a-z]*[0-9]) looks ahead to find zero or more letters until it finds a number. But when it finds, such a sequence it will continue matching from the beginning of the lookahead. Thin of it as a sort of "pre match".
[a-z0-9]{4} This part checks for four numbers or lower case characters, but we are already sure that there is at least one number there because of the lookahead.
1
In case the string is more than four characters, e.g.erty7
, your lookahead should look for 0-3 lower-case only, e.g.,(?=[a-z]{0,3}[0-9])
.
– Tanktalus
Nov 22 '18 at 18:24
Yup. Thanks for that! I fixed it with your suggestion.
– ndvo
Nov 22 '18 at 18:36
add a comment |
As your requirement says, the string should contain at least one digit and rest can be anything containing digits and lowercase alphabets of exactly 4 characters, you can use this regex,
^(?=.*d)[a-z0-9]{4}$
Explanation:
^
--> Start of input
(?=.*d)
--> Look ahead to ensure the input contains at least one digit
[a-z0-9]{4}
--> Ensures only lowercase alphabets and digits are matched in allowed character set
$
--> End of input
Demo
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%2f53436143%2fregexp-struggling%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your regex ([a-z]+|[0-9]+){4}
uses an alternation which will either match 1+ lowercase characters or 1+ digits in a capturing group and repeat that 4 times. That would also match 4 letters.
If lookarounds are supported, you could use a negative lookahead to assert that what follows are not 4 lowercase characters.
To match a string with length of 4, you could use anchors to assert the start ^
and the end $
of the string.
^(?![a-z]{4})[a-z0-9]{4}$
Regex demo
add a comment |
Your regex ([a-z]+|[0-9]+){4}
uses an alternation which will either match 1+ lowercase characters or 1+ digits in a capturing group and repeat that 4 times. That would also match 4 letters.
If lookarounds are supported, you could use a negative lookahead to assert that what follows are not 4 lowercase characters.
To match a string with length of 4, you could use anchors to assert the start ^
and the end $
of the string.
^(?![a-z]{4})[a-z0-9]{4}$
Regex demo
add a comment |
Your regex ([a-z]+|[0-9]+){4}
uses an alternation which will either match 1+ lowercase characters or 1+ digits in a capturing group and repeat that 4 times. That would also match 4 letters.
If lookarounds are supported, you could use a negative lookahead to assert that what follows are not 4 lowercase characters.
To match a string with length of 4, you could use anchors to assert the start ^
and the end $
of the string.
^(?![a-z]{4})[a-z0-9]{4}$
Regex demo
Your regex ([a-z]+|[0-9]+){4}
uses an alternation which will either match 1+ lowercase characters or 1+ digits in a capturing group and repeat that 4 times. That would also match 4 letters.
If lookarounds are supported, you could use a negative lookahead to assert that what follows are not 4 lowercase characters.
To match a string with length of 4, you could use anchors to assert the start ^
and the end $
of the string.
^(?![a-z]{4})[a-z0-9]{4}$
Regex demo
edited Nov 22 '18 at 18:20
answered Nov 22 '18 at 18:09
The fourth birdThe fourth bird
22.3k81427
22.3k81427
add a comment |
add a comment |
Your expression is matching four {4}
of whatever either any number greater than 1 of lower case letters [a-z]
or any number greater than one of digits. Therefore, your code is actually matching more than 4 of letters or digits too.
Your problem can be solved with lookaheads.
(?=[a-z]{0,3}[0-9])[a-z0-9]{4}
(?=[a-z]*[0-9]) looks ahead to find zero or more letters until it finds a number. But when it finds, such a sequence it will continue matching from the beginning of the lookahead. Thin of it as a sort of "pre match".
[a-z0-9]{4} This part checks for four numbers or lower case characters, but we are already sure that there is at least one number there because of the lookahead.
1
In case the string is more than four characters, e.g.erty7
, your lookahead should look for 0-3 lower-case only, e.g.,(?=[a-z]{0,3}[0-9])
.
– Tanktalus
Nov 22 '18 at 18:24
Yup. Thanks for that! I fixed it with your suggestion.
– ndvo
Nov 22 '18 at 18:36
add a comment |
Your expression is matching four {4}
of whatever either any number greater than 1 of lower case letters [a-z]
or any number greater than one of digits. Therefore, your code is actually matching more than 4 of letters or digits too.
Your problem can be solved with lookaheads.
(?=[a-z]{0,3}[0-9])[a-z0-9]{4}
(?=[a-z]*[0-9]) looks ahead to find zero or more letters until it finds a number. But when it finds, such a sequence it will continue matching from the beginning of the lookahead. Thin of it as a sort of "pre match".
[a-z0-9]{4} This part checks for four numbers or lower case characters, but we are already sure that there is at least one number there because of the lookahead.
1
In case the string is more than four characters, e.g.erty7
, your lookahead should look for 0-3 lower-case only, e.g.,(?=[a-z]{0,3}[0-9])
.
– Tanktalus
Nov 22 '18 at 18:24
Yup. Thanks for that! I fixed it with your suggestion.
– ndvo
Nov 22 '18 at 18:36
add a comment |
Your expression is matching four {4}
of whatever either any number greater than 1 of lower case letters [a-z]
or any number greater than one of digits. Therefore, your code is actually matching more than 4 of letters or digits too.
Your problem can be solved with lookaheads.
(?=[a-z]{0,3}[0-9])[a-z0-9]{4}
(?=[a-z]*[0-9]) looks ahead to find zero or more letters until it finds a number. But when it finds, such a sequence it will continue matching from the beginning of the lookahead. Thin of it as a sort of "pre match".
[a-z0-9]{4} This part checks for four numbers or lower case characters, but we are already sure that there is at least one number there because of the lookahead.
Your expression is matching four {4}
of whatever either any number greater than 1 of lower case letters [a-z]
or any number greater than one of digits. Therefore, your code is actually matching more than 4 of letters or digits too.
Your problem can be solved with lookaheads.
(?=[a-z]{0,3}[0-9])[a-z0-9]{4}
(?=[a-z]*[0-9]) looks ahead to find zero or more letters until it finds a number. But when it finds, such a sequence it will continue matching from the beginning of the lookahead. Thin of it as a sort of "pre match".
[a-z0-9]{4} This part checks for four numbers or lower case characters, but we are already sure that there is at least one number there because of the lookahead.
edited Nov 22 '18 at 18:37
answered Nov 22 '18 at 18:11
ndvondvo
381210
381210
1
In case the string is more than four characters, e.g.erty7
, your lookahead should look for 0-3 lower-case only, e.g.,(?=[a-z]{0,3}[0-9])
.
– Tanktalus
Nov 22 '18 at 18:24
Yup. Thanks for that! I fixed it with your suggestion.
– ndvo
Nov 22 '18 at 18:36
add a comment |
1
In case the string is more than four characters, e.g.erty7
, your lookahead should look for 0-3 lower-case only, e.g.,(?=[a-z]{0,3}[0-9])
.
– Tanktalus
Nov 22 '18 at 18:24
Yup. Thanks for that! I fixed it with your suggestion.
– ndvo
Nov 22 '18 at 18:36
1
1
In case the string is more than four characters, e.g.
erty7
, your lookahead should look for 0-3 lower-case only, e.g., (?=[a-z]{0,3}[0-9])
.– Tanktalus
Nov 22 '18 at 18:24
In case the string is more than four characters, e.g.
erty7
, your lookahead should look for 0-3 lower-case only, e.g., (?=[a-z]{0,3}[0-9])
.– Tanktalus
Nov 22 '18 at 18:24
Yup. Thanks for that! I fixed it with your suggestion.
– ndvo
Nov 22 '18 at 18:36
Yup. Thanks for that! I fixed it with your suggestion.
– ndvo
Nov 22 '18 at 18:36
add a comment |
As your requirement says, the string should contain at least one digit and rest can be anything containing digits and lowercase alphabets of exactly 4 characters, you can use this regex,
^(?=.*d)[a-z0-9]{4}$
Explanation:
^
--> Start of input
(?=.*d)
--> Look ahead to ensure the input contains at least one digit
[a-z0-9]{4}
--> Ensures only lowercase alphabets and digits are matched in allowed character set
$
--> End of input
Demo
add a comment |
As your requirement says, the string should contain at least one digit and rest can be anything containing digits and lowercase alphabets of exactly 4 characters, you can use this regex,
^(?=.*d)[a-z0-9]{4}$
Explanation:
^
--> Start of input
(?=.*d)
--> Look ahead to ensure the input contains at least one digit
[a-z0-9]{4}
--> Ensures only lowercase alphabets and digits are matched in allowed character set
$
--> End of input
Demo
add a comment |
As your requirement says, the string should contain at least one digit and rest can be anything containing digits and lowercase alphabets of exactly 4 characters, you can use this regex,
^(?=.*d)[a-z0-9]{4}$
Explanation:
^
--> Start of input
(?=.*d)
--> Look ahead to ensure the input contains at least one digit
[a-z0-9]{4}
--> Ensures only lowercase alphabets and digits are matched in allowed character set
$
--> End of input
Demo
As your requirement says, the string should contain at least one digit and rest can be anything containing digits and lowercase alphabets of exactly 4 characters, you can use this regex,
^(?=.*d)[a-z0-9]{4}$
Explanation:
^
--> Start of input
(?=.*d)
--> Look ahead to ensure the input contains at least one digit
[a-z0-9]{4}
--> Ensures only lowercase alphabets and digits are matched in allowed character set
$
--> End of input
Demo
answered Nov 22 '18 at 20:03
Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi
7,0352927
7,0352927
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%2f53436143%2fregexp-struggling%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
1
what is the language?
– Ulugbek Umirov
Nov 22 '18 at 18:06