C regexec returning false positives
I am implementing a "bank" software and I have to take in an acceptable username, pin, and balance when creating a new user.
BANK: create-user bob 1234 11111
would be an example of a correct input
BANK: create-user bob0101 1234 11111
would be incorrect, basically a single word username, a 4 digit pin, and some integer.
regexec returns 0 for both username inputs even though it absolutely shouldn't.
the code is currently
regex_t regex_s, regex_i, regex_p;
int reti_s = regcomp(®ex_s, "[a-z A-Z]+", REG_EXTENDED);
.
.
.
reti_s = regexec(®ex_s, cmd[1], 0, NULL, 0);
where cmd[1] is the username
I don't have a lot of experience with C regex but I know this shouldn't allow numbers, any help would be great thanks!
c regex
add a comment |
I am implementing a "bank" software and I have to take in an acceptable username, pin, and balance when creating a new user.
BANK: create-user bob 1234 11111
would be an example of a correct input
BANK: create-user bob0101 1234 11111
would be incorrect, basically a single word username, a 4 digit pin, and some integer.
regexec returns 0 for both username inputs even though it absolutely shouldn't.
the code is currently
regex_t regex_s, regex_i, regex_p;
int reti_s = regcomp(®ex_s, "[a-z A-Z]+", REG_EXTENDED);
.
.
.
reti_s = regexec(®ex_s, cmd[1], 0, NULL, 0);
where cmd[1] is the username
I don't have a lot of experience with C regex but I know this shouldn't allow numbers, any help would be great thanks!
c regex
If you want the username to contain only letters, andcmd[1]
is the full username and only the username, then maybe you just need to use^
and$
anchors?
– CertainPerformance
Nov 10 at 2:19
Thanks so much it's working in most cases now!
– Ronoc Eroom
Nov 10 at 2:23
why is the second example incorrect? you don't explain why. Isn'tbob0101
a single word name? cannot it have digits? PLEASE EXPLAIN.
– Luis Colorado
Nov 15 at 10:13
Your sample regexp allows spaces in a name.... this will make"bob "
valid!
– Luis Colorado
Nov 15 at 10:16
Yes bob can’t have any numbers in it, that’s why the regex is just [a-z A-Z]+
– Ronoc Eroom
Nov 15 at 12:07
add a comment |
I am implementing a "bank" software and I have to take in an acceptable username, pin, and balance when creating a new user.
BANK: create-user bob 1234 11111
would be an example of a correct input
BANK: create-user bob0101 1234 11111
would be incorrect, basically a single word username, a 4 digit pin, and some integer.
regexec returns 0 for both username inputs even though it absolutely shouldn't.
the code is currently
regex_t regex_s, regex_i, regex_p;
int reti_s = regcomp(®ex_s, "[a-z A-Z]+", REG_EXTENDED);
.
.
.
reti_s = regexec(®ex_s, cmd[1], 0, NULL, 0);
where cmd[1] is the username
I don't have a lot of experience with C regex but I know this shouldn't allow numbers, any help would be great thanks!
c regex
I am implementing a "bank" software and I have to take in an acceptable username, pin, and balance when creating a new user.
BANK: create-user bob 1234 11111
would be an example of a correct input
BANK: create-user bob0101 1234 11111
would be incorrect, basically a single word username, a 4 digit pin, and some integer.
regexec returns 0 for both username inputs even though it absolutely shouldn't.
the code is currently
regex_t regex_s, regex_i, regex_p;
int reti_s = regcomp(®ex_s, "[a-z A-Z]+", REG_EXTENDED);
.
.
.
reti_s = regexec(®ex_s, cmd[1], 0, NULL, 0);
where cmd[1] is the username
I don't have a lot of experience with C regex but I know this shouldn't allow numbers, any help would be great thanks!
c regex
c regex
asked Nov 10 at 2:12
Ronoc Eroom
145
145
If you want the username to contain only letters, andcmd[1]
is the full username and only the username, then maybe you just need to use^
and$
anchors?
– CertainPerformance
Nov 10 at 2:19
Thanks so much it's working in most cases now!
– Ronoc Eroom
Nov 10 at 2:23
why is the second example incorrect? you don't explain why. Isn'tbob0101
a single word name? cannot it have digits? PLEASE EXPLAIN.
– Luis Colorado
Nov 15 at 10:13
Your sample regexp allows spaces in a name.... this will make"bob "
valid!
– Luis Colorado
Nov 15 at 10:16
Yes bob can’t have any numbers in it, that’s why the regex is just [a-z A-Z]+
– Ronoc Eroom
Nov 15 at 12:07
add a comment |
If you want the username to contain only letters, andcmd[1]
is the full username and only the username, then maybe you just need to use^
and$
anchors?
– CertainPerformance
Nov 10 at 2:19
Thanks so much it's working in most cases now!
– Ronoc Eroom
Nov 10 at 2:23
why is the second example incorrect? you don't explain why. Isn'tbob0101
a single word name? cannot it have digits? PLEASE EXPLAIN.
– Luis Colorado
Nov 15 at 10:13
Your sample regexp allows spaces in a name.... this will make"bob "
valid!
– Luis Colorado
Nov 15 at 10:16
Yes bob can’t have any numbers in it, that’s why the regex is just [a-z A-Z]+
– Ronoc Eroom
Nov 15 at 12:07
If you want the username to contain only letters, and
cmd[1]
is the full username and only the username, then maybe you just need to use ^
and $
anchors?– CertainPerformance
Nov 10 at 2:19
If you want the username to contain only letters, and
cmd[1]
is the full username and only the username, then maybe you just need to use ^
and $
anchors?– CertainPerformance
Nov 10 at 2:19
Thanks so much it's working in most cases now!
– Ronoc Eroom
Nov 10 at 2:23
Thanks so much it's working in most cases now!
– Ronoc Eroom
Nov 10 at 2:23
why is the second example incorrect? you don't explain why. Isn't
bob0101
a single word name? cannot it have digits? PLEASE EXPLAIN.– Luis Colorado
Nov 15 at 10:13
why is the second example incorrect? you don't explain why. Isn't
bob0101
a single word name? cannot it have digits? PLEASE EXPLAIN.– Luis Colorado
Nov 15 at 10:13
Your sample regexp allows spaces in a name.... this will make
"bob "
valid!– Luis Colorado
Nov 15 at 10:16
Your sample regexp allows spaces in a name.... this will make
"bob "
valid!– Luis Colorado
Nov 15 at 10:16
Yes bob can’t have any numbers in it, that’s why the regex is just [a-z A-Z]+
– Ronoc Eroom
Nov 15 at 12:07
Yes bob can’t have any numbers in it, that’s why the regex is just [a-z A-Z]+
– Ronoc Eroom
Nov 15 at 12:07
add a comment |
2 Answers
2
active
oldest
votes
If you want to match letters-only characters, you have to use something like this:
int reti_s = regcomp(®ex_s, "[a-z]+", REG_EXTENDED|REG_ICASE);
I added the REG_ICASE flag according to https://linux.die.net/man/3/regcomp.
1
I don't see how this answers the question.
– R..
Nov 10 at 4:46
I don't see also the need ofREG_ICASE
orREG_EXTENDED
flags. Why use extended regular expressions if you are not using grouping at all?
– Luis Colorado
Nov 15 at 10:18
I use REG_EXTENDED for completeness, because is the preferred syntax along other languages. REG_ICASE will match things like Bob which perfectly could be a valid username.
– Juan Ignacio Sánchez
Nov 15 at 12:33
add a comment |
regexec
just returns 0
if the regexp simply matches. If you want to extract information, you need to use an array of regmatch_t
to store the matching of subexpressions. Or if you are only interested in global matches, one array of just one regmatch_t
element (to get the info about the match).
As the manual page says:
The 0th member of the pmatch array is filled in to indicate what substring of string was matched by the entire RE.
and you have to use the rm_so
and rm_eo
fields of regmatch_t
to get the initial position and the end position of the matched part of the string.
Thanks for the answer! I didn’t want to store matches though I just needed to check if a single word matches the format it needs to be. For example the pin value must be a 4 digit number. It’s been fixed now thanks to CertainPerformance
– Ronoc Eroom
Nov 15 at 12:10
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%2f53235452%2fc-regexec-returning-false-positives%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
If you want to match letters-only characters, you have to use something like this:
int reti_s = regcomp(®ex_s, "[a-z]+", REG_EXTENDED|REG_ICASE);
I added the REG_ICASE flag according to https://linux.die.net/man/3/regcomp.
1
I don't see how this answers the question.
– R..
Nov 10 at 4:46
I don't see also the need ofREG_ICASE
orREG_EXTENDED
flags. Why use extended regular expressions if you are not using grouping at all?
– Luis Colorado
Nov 15 at 10:18
I use REG_EXTENDED for completeness, because is the preferred syntax along other languages. REG_ICASE will match things like Bob which perfectly could be a valid username.
– Juan Ignacio Sánchez
Nov 15 at 12:33
add a comment |
If you want to match letters-only characters, you have to use something like this:
int reti_s = regcomp(®ex_s, "[a-z]+", REG_EXTENDED|REG_ICASE);
I added the REG_ICASE flag according to https://linux.die.net/man/3/regcomp.
1
I don't see how this answers the question.
– R..
Nov 10 at 4:46
I don't see also the need ofREG_ICASE
orREG_EXTENDED
flags. Why use extended regular expressions if you are not using grouping at all?
– Luis Colorado
Nov 15 at 10:18
I use REG_EXTENDED for completeness, because is the preferred syntax along other languages. REG_ICASE will match things like Bob which perfectly could be a valid username.
– Juan Ignacio Sánchez
Nov 15 at 12:33
add a comment |
If you want to match letters-only characters, you have to use something like this:
int reti_s = regcomp(®ex_s, "[a-z]+", REG_EXTENDED|REG_ICASE);
I added the REG_ICASE flag according to https://linux.die.net/man/3/regcomp.
If you want to match letters-only characters, you have to use something like this:
int reti_s = regcomp(®ex_s, "[a-z]+", REG_EXTENDED|REG_ICASE);
I added the REG_ICASE flag according to https://linux.die.net/man/3/regcomp.
answered Nov 10 at 2:34
Juan Ignacio Sánchez
316111
316111
1
I don't see how this answers the question.
– R..
Nov 10 at 4:46
I don't see also the need ofREG_ICASE
orREG_EXTENDED
flags. Why use extended regular expressions if you are not using grouping at all?
– Luis Colorado
Nov 15 at 10:18
I use REG_EXTENDED for completeness, because is the preferred syntax along other languages. REG_ICASE will match things like Bob which perfectly could be a valid username.
– Juan Ignacio Sánchez
Nov 15 at 12:33
add a comment |
1
I don't see how this answers the question.
– R..
Nov 10 at 4:46
I don't see also the need ofREG_ICASE
orREG_EXTENDED
flags. Why use extended regular expressions if you are not using grouping at all?
– Luis Colorado
Nov 15 at 10:18
I use REG_EXTENDED for completeness, because is the preferred syntax along other languages. REG_ICASE will match things like Bob which perfectly could be a valid username.
– Juan Ignacio Sánchez
Nov 15 at 12:33
1
1
I don't see how this answers the question.
– R..
Nov 10 at 4:46
I don't see how this answers the question.
– R..
Nov 10 at 4:46
I don't see also the need of
REG_ICASE
or REG_EXTENDED
flags. Why use extended regular expressions if you are not using grouping at all?– Luis Colorado
Nov 15 at 10:18
I don't see also the need of
REG_ICASE
or REG_EXTENDED
flags. Why use extended regular expressions if you are not using grouping at all?– Luis Colorado
Nov 15 at 10:18
I use REG_EXTENDED for completeness, because is the preferred syntax along other languages. REG_ICASE will match things like Bob which perfectly could be a valid username.
– Juan Ignacio Sánchez
Nov 15 at 12:33
I use REG_EXTENDED for completeness, because is the preferred syntax along other languages. REG_ICASE will match things like Bob which perfectly could be a valid username.
– Juan Ignacio Sánchez
Nov 15 at 12:33
add a comment |
regexec
just returns 0
if the regexp simply matches. If you want to extract information, you need to use an array of regmatch_t
to store the matching of subexpressions. Or if you are only interested in global matches, one array of just one regmatch_t
element (to get the info about the match).
As the manual page says:
The 0th member of the pmatch array is filled in to indicate what substring of string was matched by the entire RE.
and you have to use the rm_so
and rm_eo
fields of regmatch_t
to get the initial position and the end position of the matched part of the string.
Thanks for the answer! I didn’t want to store matches though I just needed to check if a single word matches the format it needs to be. For example the pin value must be a 4 digit number. It’s been fixed now thanks to CertainPerformance
– Ronoc Eroom
Nov 15 at 12:10
add a comment |
regexec
just returns 0
if the regexp simply matches. If you want to extract information, you need to use an array of regmatch_t
to store the matching of subexpressions. Or if you are only interested in global matches, one array of just one regmatch_t
element (to get the info about the match).
As the manual page says:
The 0th member of the pmatch array is filled in to indicate what substring of string was matched by the entire RE.
and you have to use the rm_so
and rm_eo
fields of regmatch_t
to get the initial position and the end position of the matched part of the string.
Thanks for the answer! I didn’t want to store matches though I just needed to check if a single word matches the format it needs to be. For example the pin value must be a 4 digit number. It’s been fixed now thanks to CertainPerformance
– Ronoc Eroom
Nov 15 at 12:10
add a comment |
regexec
just returns 0
if the regexp simply matches. If you want to extract information, you need to use an array of regmatch_t
to store the matching of subexpressions. Or if you are only interested in global matches, one array of just one regmatch_t
element (to get the info about the match).
As the manual page says:
The 0th member of the pmatch array is filled in to indicate what substring of string was matched by the entire RE.
and you have to use the rm_so
and rm_eo
fields of regmatch_t
to get the initial position and the end position of the matched part of the string.
regexec
just returns 0
if the regexp simply matches. If you want to extract information, you need to use an array of regmatch_t
to store the matching of subexpressions. Or if you are only interested in global matches, one array of just one regmatch_t
element (to get the info about the match).
As the manual page says:
The 0th member of the pmatch array is filled in to indicate what substring of string was matched by the entire RE.
and you have to use the rm_so
and rm_eo
fields of regmatch_t
to get the initial position and the end position of the matched part of the string.
answered Nov 15 at 10:25
Luis Colorado
4,0641718
4,0641718
Thanks for the answer! I didn’t want to store matches though I just needed to check if a single word matches the format it needs to be. For example the pin value must be a 4 digit number. It’s been fixed now thanks to CertainPerformance
– Ronoc Eroom
Nov 15 at 12:10
add a comment |
Thanks for the answer! I didn’t want to store matches though I just needed to check if a single word matches the format it needs to be. For example the pin value must be a 4 digit number. It’s been fixed now thanks to CertainPerformance
– Ronoc Eroom
Nov 15 at 12:10
Thanks for the answer! I didn’t want to store matches though I just needed to check if a single word matches the format it needs to be. For example the pin value must be a 4 digit number. It’s been fixed now thanks to CertainPerformance
– Ronoc Eroom
Nov 15 at 12:10
Thanks for the answer! I didn’t want to store matches though I just needed to check if a single word matches the format it needs to be. For example the pin value must be a 4 digit number. It’s been fixed now thanks to CertainPerformance
– Ronoc Eroom
Nov 15 at 12:10
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%2f53235452%2fc-regexec-returning-false-positives%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
If you want the username to contain only letters, and
cmd[1]
is the full username and only the username, then maybe you just need to use^
and$
anchors?– CertainPerformance
Nov 10 at 2:19
Thanks so much it's working in most cases now!
– Ronoc Eroom
Nov 10 at 2:23
why is the second example incorrect? you don't explain why. Isn't
bob0101
a single word name? cannot it have digits? PLEASE EXPLAIN.– Luis Colorado
Nov 15 at 10:13
Your sample regexp allows spaces in a name.... this will make
"bob "
valid!– Luis Colorado
Nov 15 at 10:16
Yes bob can’t have any numbers in it, that’s why the regex is just [a-z A-Z]+
– Ronoc Eroom
Nov 15 at 12:07