Regex number and hyphenHow to write a regex for digits separated by - in pythonWhat is the best regular expression to check if a string is a valid URL?Regex for numbers onlyRegular Expression for alphanumeric and underscoresRegular expression to match a line that doesn't contain a wordHow do you access the matched groups in a JavaScript regular expression?How do you use a variable in a regular expression?How to negate specific word in regex?How to match “anything up until this sequence of characters” in a regular expression?Regex - Should hyphens be escaped?How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
Where can I get macOS Catalina Beta version?
Meaning of もてり and use of が
Skipping over failed imports until they are needed (if ever)
Are there any features that help with the roll to avoid the destruction of a Wand of Fireballs when using the last charge?
Does the Pi 4 resolve the Ethernet+USB bottleneck issue of past versions?
Can you sign using a digital signature itself?
Can the passive "être + verbe" sometimes mean the past?
Being paid less than a "junior" colleague
How can a valley surrounded by mountains be fertile and rainy?
Why are there so many religions and gods?
Can the UK Prime Minister immediately withdraw the country from the EU without backing from parliament?
Could human civilization live 150 years in a nuclear-powered aircraft carrier colony without resorting to mass killing/ cannibalism?
Can a police officer film me on their personal device in my own home?
How can I reduce the sound of rain on a range hood vent?
Reverse of diffraction
What does grep -v "grep" mean and do?
Do space suits measure "methane" levels or other biological gases?
Is it bad to describe a character long after their introduction?
Generate and graph the Recamán Sequence
Can 'leave' mean 'forget'?
Is there a nice way to assign std::minmax(a, b) to std::tie(a, b)?
Question on oracles
3D nonogram, beginner's edition
In the context of a differentiator circuit, what is a “current-sensing resistor”?
Regex number and hyphen
How to write a regex for digits separated by - in pythonWhat is the best regular expression to check if a string is a valid URL?Regex for numbers onlyRegular Expression for alphanumeric and underscoresRegular expression to match a line that doesn't contain a wordHow do you access the matched groups in a JavaScript regular expression?How do you use a variable in a regular expression?How to negate specific word in regex?How to match “anything up until this sequence of characters” in a regular expression?Regex - Should hyphens be escaped?How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm trying to match number with regular expression like:
34-7878-3523-4233
with this:
^[0-9][0-9-]*-[0-9-]*[0-9]$
But the expression also allow
34--34--------88
So how can I allow only one hyphen between the number?
regex
add a comment |
I'm trying to match number with regular expression like:
34-7878-3523-4233
with this:
^[0-9][0-9-]*-[0-9-]*[0-9]$
But the expression also allow
34--34--------88
So how can I allow only one hyphen between the number?
regex
add a comment |
I'm trying to match number with regular expression like:
34-7878-3523-4233
with this:
^[0-9][0-9-]*-[0-9-]*[0-9]$
But the expression also allow
34--34--------88
So how can I allow only one hyphen between the number?
regex
I'm trying to match number with regular expression like:
34-7878-3523-4233
with this:
^[0-9][0-9-]*-[0-9-]*[0-9]$
But the expression also allow
34--34--------88
So how can I allow only one hyphen between the number?
regex
regex
asked Dec 25 '12 at 13:02
vusanvusan
3,7544 gold badges33 silver badges73 bronze badges
3,7544 gold badges33 silver badges73 bronze badges
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Your regex:
See it in action: Regexr.com
^[0-9]+(-[0-9]+)+$
Matches:
1-2
1-2-3
Doesn't match:
1
1-
1-2-
1-2----3
1---3
Thanks but for me there should exist at least one hyphen
– vusan
Dec 25 '12 at 13:09
@vusan OK, updated the regex.
– mmdemirbas
Dec 25 '12 at 13:10
add a comment |
That's because, you have included the hyphen in the allowed characters in your character class. You should have it outside.
You can try something like this: -
^([0-9]+-)*[0-9]+$
Now this will match 0 or more repetition of some digits followed by a hyphen. Then one or more digits at the end.
It just allow with one hyphen with single digit like3-3-3
. I want to match like232424-42-2455
– vusan
Dec 25 '12 at 13:07
@vusan. No it will match multiple digit now. I forgot to put quantifiers.
– Rohit Jain
Dec 25 '12 at 13:07
add a comment |
Use the normal*(special normal*)*
pattern:
^[0-9]+(-[0-9]+)+$
where normal
is [0-9]
and special
is -
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%2f14030873%2fregex-number-and-hyphen%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:
See it in action: Regexr.com
^[0-9]+(-[0-9]+)+$
Matches:
1-2
1-2-3
Doesn't match:
1
1-
1-2-
1-2----3
1---3
Thanks but for me there should exist at least one hyphen
– vusan
Dec 25 '12 at 13:09
@vusan OK, updated the regex.
– mmdemirbas
Dec 25 '12 at 13:10
add a comment |
Your regex:
See it in action: Regexr.com
^[0-9]+(-[0-9]+)+$
Matches:
1-2
1-2-3
Doesn't match:
1
1-
1-2-
1-2----3
1---3
Thanks but for me there should exist at least one hyphen
– vusan
Dec 25 '12 at 13:09
@vusan OK, updated the regex.
– mmdemirbas
Dec 25 '12 at 13:10
add a comment |
Your regex:
See it in action: Regexr.com
^[0-9]+(-[0-9]+)+$
Matches:
1-2
1-2-3
Doesn't match:
1
1-
1-2-
1-2----3
1---3
Your regex:
See it in action: Regexr.com
^[0-9]+(-[0-9]+)+$
Matches:
1-2
1-2-3
Doesn't match:
1
1-
1-2-
1-2----3
1---3
edited Dec 25 '12 at 19:12
answered Dec 25 '12 at 13:05
mmdemirbasmmdemirbas
7,2162 gold badges34 silver badges50 bronze badges
7,2162 gold badges34 silver badges50 bronze badges
Thanks but for me there should exist at least one hyphen
– vusan
Dec 25 '12 at 13:09
@vusan OK, updated the regex.
– mmdemirbas
Dec 25 '12 at 13:10
add a comment |
Thanks but for me there should exist at least one hyphen
– vusan
Dec 25 '12 at 13:09
@vusan OK, updated the regex.
– mmdemirbas
Dec 25 '12 at 13:10
Thanks but for me there should exist at least one hyphen
– vusan
Dec 25 '12 at 13:09
Thanks but for me there should exist at least one hyphen
– vusan
Dec 25 '12 at 13:09
@vusan OK, updated the regex.
– mmdemirbas
Dec 25 '12 at 13:10
@vusan OK, updated the regex.
– mmdemirbas
Dec 25 '12 at 13:10
add a comment |
That's because, you have included the hyphen in the allowed characters in your character class. You should have it outside.
You can try something like this: -
^([0-9]+-)*[0-9]+$
Now this will match 0 or more repetition of some digits followed by a hyphen. Then one or more digits at the end.
It just allow with one hyphen with single digit like3-3-3
. I want to match like232424-42-2455
– vusan
Dec 25 '12 at 13:07
@vusan. No it will match multiple digit now. I forgot to put quantifiers.
– Rohit Jain
Dec 25 '12 at 13:07
add a comment |
That's because, you have included the hyphen in the allowed characters in your character class. You should have it outside.
You can try something like this: -
^([0-9]+-)*[0-9]+$
Now this will match 0 or more repetition of some digits followed by a hyphen. Then one or more digits at the end.
It just allow with one hyphen with single digit like3-3-3
. I want to match like232424-42-2455
– vusan
Dec 25 '12 at 13:07
@vusan. No it will match multiple digit now. I forgot to put quantifiers.
– Rohit Jain
Dec 25 '12 at 13:07
add a comment |
That's because, you have included the hyphen in the allowed characters in your character class. You should have it outside.
You can try something like this: -
^([0-9]+-)*[0-9]+$
Now this will match 0 or more repetition of some digits followed by a hyphen. Then one or more digits at the end.
That's because, you have included the hyphen in the allowed characters in your character class. You should have it outside.
You can try something like this: -
^([0-9]+-)*[0-9]+$
Now this will match 0 or more repetition of some digits followed by a hyphen. Then one or more digits at the end.
answered Dec 25 '12 at 13:04
Rohit JainRohit Jain
172k39 gold badges327 silver badges453 bronze badges
172k39 gold badges327 silver badges453 bronze badges
It just allow with one hyphen with single digit like3-3-3
. I want to match like232424-42-2455
– vusan
Dec 25 '12 at 13:07
@vusan. No it will match multiple digit now. I forgot to put quantifiers.
– Rohit Jain
Dec 25 '12 at 13:07
add a comment |
It just allow with one hyphen with single digit like3-3-3
. I want to match like232424-42-2455
– vusan
Dec 25 '12 at 13:07
@vusan. No it will match multiple digit now. I forgot to put quantifiers.
– Rohit Jain
Dec 25 '12 at 13:07
It just allow with one hyphen with single digit like
3-3-3
. I want to match like 232424-42-2455
– vusan
Dec 25 '12 at 13:07
It just allow with one hyphen with single digit like
3-3-3
. I want to match like 232424-42-2455
– vusan
Dec 25 '12 at 13:07
@vusan. No it will match multiple digit now. I forgot to put quantifiers.
– Rohit Jain
Dec 25 '12 at 13:07
@vusan. No it will match multiple digit now. I forgot to put quantifiers.
– Rohit Jain
Dec 25 '12 at 13:07
add a comment |
Use the normal*(special normal*)*
pattern:
^[0-9]+(-[0-9]+)+$
where normal
is [0-9]
and special
is -
add a comment |
Use the normal*(special normal*)*
pattern:
^[0-9]+(-[0-9]+)+$
where normal
is [0-9]
and special
is -
add a comment |
Use the normal*(special normal*)*
pattern:
^[0-9]+(-[0-9]+)+$
where normal
is [0-9]
and special
is -
Use the normal*(special normal*)*
pattern:
^[0-9]+(-[0-9]+)+$
where normal
is [0-9]
and special
is -
answered Dec 25 '12 at 13:05
fgefge
93k18 gold badges196 silver badges285 bronze badges
93k18 gold badges196 silver badges285 bronze badges
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%2f14030873%2fregex-number-and-hyphen%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