C regexec returning false positives










0














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(&regex_s, "[a-z A-Z]+", REG_EXTENDED);
.
.
.
reti_s = regexec(&regex_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!










share|improve this question





















  • 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















0














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(&regex_s, "[a-z A-Z]+", REG_EXTENDED);
.
.
.
reti_s = regexec(&regex_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!










share|improve this question





















  • 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













0












0








0







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(&regex_s, "[a-z A-Z]+", REG_EXTENDED);
.
.
.
reti_s = regexec(&regex_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!










share|improve this question













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(&regex_s, "[a-z A-Z]+", REG_EXTENDED);
.
.
.
reti_s = regexec(&regex_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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 2:12









Ronoc Eroom

145




145











  • 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
















  • 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















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












2 Answers
2






active

oldest

votes


















0














If you want to match letters-only characters, you have to use something like this:



int reti_s = regcomp(&regex_s, "[a-z]+", REG_EXTENDED|REG_ICASE);



I added the REG_ICASE flag according to https://linux.die.net/man/3/regcomp.






share|improve this answer
















  • 1




    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 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


















0














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.






share|improve this answer




















  • 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










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
);



);













draft saved

draft discarded


















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









0














If you want to match letters-only characters, you have to use something like this:



int reti_s = regcomp(&regex_s, "[a-z]+", REG_EXTENDED|REG_ICASE);



I added the REG_ICASE flag according to https://linux.die.net/man/3/regcomp.






share|improve this answer
















  • 1




    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 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















0














If you want to match letters-only characters, you have to use something like this:



int reti_s = regcomp(&regex_s, "[a-z]+", REG_EXTENDED|REG_ICASE);



I added the REG_ICASE flag according to https://linux.die.net/man/3/regcomp.






share|improve this answer
















  • 1




    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 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













0












0








0






If you want to match letters-only characters, you have to use something like this:



int reti_s = regcomp(&regex_s, "[a-z]+", REG_EXTENDED|REG_ICASE);



I added the REG_ICASE flag according to https://linux.die.net/man/3/regcomp.






share|improve this answer












If you want to match letters-only characters, you have to use something like this:



int reti_s = regcomp(&regex_s, "[a-z]+", REG_EXTENDED|REG_ICASE);



I added the REG_ICASE flag according to https://linux.die.net/man/3/regcomp.







share|improve this answer












share|improve this answer



share|improve this answer










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 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












  • 1




    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 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













0














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.






share|improve this answer




















  • 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















0














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.






share|improve this answer




















  • 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













0












0








0






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)