Need regex for a string having characters followed by even number of digits
Can anyone tell how I can write regex for a string that take one or more alphanumeric character followed by an even number of digits?
Valid:
a11a1121
bbbb11a1121
Invalid:
a11a1
I have tried ^[a-zA-Z*20-9]*$
but it is always giving true.
Can you please help in this regard?
java regex
add a comment |
Can anyone tell how I can write regex for a string that take one or more alphanumeric character followed by an even number of digits?
Valid:
a11a1121
bbbb11a1121
Invalid:
a11a1
I have tried ^[a-zA-Z*20-9]*$
but it is always giving true.
Can you please help in this regard?
java regex
2
Possible duplicate of Learning Regular Expressions
– Biffen
Nov 12 at 11:52
add a comment |
Can anyone tell how I can write regex for a string that take one or more alphanumeric character followed by an even number of digits?
Valid:
a11a1121
bbbb11a1121
Invalid:
a11a1
I have tried ^[a-zA-Z*20-9]*$
but it is always giving true.
Can you please help in this regard?
java regex
Can anyone tell how I can write regex for a string that take one or more alphanumeric character followed by an even number of digits?
Valid:
a11a1121
bbbb11a1121
Invalid:
a11a1
I have tried ^[a-zA-Z*20-9]*$
but it is always giving true.
Can you please help in this regard?
java regex
java regex
edited Nov 12 at 11:53
Biffen
4,16352129
4,16352129
asked Nov 12 at 11:47
Icche Guri
96211130
96211130
2
Possible duplicate of Learning Regular Expressions
– Biffen
Nov 12 at 11:52
add a comment |
2
Possible duplicate of Learning Regular Expressions
– Biffen
Nov 12 at 11:52
2
2
Possible duplicate of Learning Regular Expressions
– Biffen
Nov 12 at 11:52
Possible duplicate of Learning Regular Expressions
– Biffen
Nov 12 at 11:52
add a comment |
2 Answers
2
active
oldest
votes
The regex that you have mentioned will search for any number of [either a-z, or A-Z or 2 or 0-9]
You can break down your requirement to groups and then handle it accordingly.
Like you require at least one character. so you start with ^([a-zA-Z]+)$
Then you need numbers in the multiple of 2. so you add ^([a-zA-Z]+(dd)+)$
Now you need any number of combination of these. So the exp becomes: ^([a-zA-Z]+(dd)+)*$
You can use online tools like regex101 for these purpose. The provided regex in action here
add a comment |
You can achieve it with this regexp: ^[a-z0-9]*[a-z]+([0-9]{2})*$
Explanation :
[a-z0-9]*[a-z]+
: a string of at least one character terminated by a non digit one
([0-9]{2})*
: an odd sequence of digits (0 or 2*n digits). If the even sequence cannot be null, use([0-9]{2})+
instead.
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%2f53261522%2fneed-regex-for-a-string-having-characters-followed-by-even-number-of-digits%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
The regex that you have mentioned will search for any number of [either a-z, or A-Z or 2 or 0-9]
You can break down your requirement to groups and then handle it accordingly.
Like you require at least one character. so you start with ^([a-zA-Z]+)$
Then you need numbers in the multiple of 2. so you add ^([a-zA-Z]+(dd)+)$
Now you need any number of combination of these. So the exp becomes: ^([a-zA-Z]+(dd)+)*$
You can use online tools like regex101 for these purpose. The provided regex in action here
add a comment |
The regex that you have mentioned will search for any number of [either a-z, or A-Z or 2 or 0-9]
You can break down your requirement to groups and then handle it accordingly.
Like you require at least one character. so you start with ^([a-zA-Z]+)$
Then you need numbers in the multiple of 2. so you add ^([a-zA-Z]+(dd)+)$
Now you need any number of combination of these. So the exp becomes: ^([a-zA-Z]+(dd)+)*$
You can use online tools like regex101 for these purpose. The provided regex in action here
add a comment |
The regex that you have mentioned will search for any number of [either a-z, or A-Z or 2 or 0-9]
You can break down your requirement to groups and then handle it accordingly.
Like you require at least one character. so you start with ^([a-zA-Z]+)$
Then you need numbers in the multiple of 2. so you add ^([a-zA-Z]+(dd)+)$
Now you need any number of combination of these. So the exp becomes: ^([a-zA-Z]+(dd)+)*$
You can use online tools like regex101 for these purpose. The provided regex in action here
The regex that you have mentioned will search for any number of [either a-z, or A-Z or 2 or 0-9]
You can break down your requirement to groups and then handle it accordingly.
Like you require at least one character. so you start with ^([a-zA-Z]+)$
Then you need numbers in the multiple of 2. so you add ^([a-zA-Z]+(dd)+)$
Now you need any number of combination of these. So the exp becomes: ^([a-zA-Z]+(dd)+)*$
You can use online tools like regex101 for these purpose. The provided regex in action here
answered Nov 12 at 12:01
suvartheec
1,121917
1,121917
add a comment |
add a comment |
You can achieve it with this regexp: ^[a-z0-9]*[a-z]+([0-9]{2})*$
Explanation :
[a-z0-9]*[a-z]+
: a string of at least one character terminated by a non digit one
([0-9]{2})*
: an odd sequence of digits (0 or 2*n digits). If the even sequence cannot be null, use([0-9]{2})+
instead.
add a comment |
You can achieve it with this regexp: ^[a-z0-9]*[a-z]+([0-9]{2})*$
Explanation :
[a-z0-9]*[a-z]+
: a string of at least one character terminated by a non digit one
([0-9]{2})*
: an odd sequence of digits (0 or 2*n digits). If the even sequence cannot be null, use([0-9]{2})+
instead.
add a comment |
You can achieve it with this regexp: ^[a-z0-9]*[a-z]+([0-9]{2})*$
Explanation :
[a-z0-9]*[a-z]+
: a string of at least one character terminated by a non digit one
([0-9]{2})*
: an odd sequence of digits (0 or 2*n digits). If the even sequence cannot be null, use([0-9]{2})+
instead.
You can achieve it with this regexp: ^[a-z0-9]*[a-z]+([0-9]{2})*$
Explanation :
[a-z0-9]*[a-z]+
: a string of at least one character terminated by a non digit one
([0-9]{2})*
: an odd sequence of digits (0 or 2*n digits). If the even sequence cannot be null, use([0-9]{2})+
instead.
edited Nov 12 at 12:29
answered Nov 12 at 11:54
Amessihel
1,9631723
1,9631723
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.
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%2f53261522%2fneed-regex-for-a-string-having-characters-followed-by-even-number-of-digits%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
2
Possible duplicate of Learning Regular Expressions
– Biffen
Nov 12 at 11:52