Reputation: 2521
I the below items i want to only detect the valid items with regular expression. Space in word means invalid, # sign means invalid, Starting word with number is invalid.
Invalid : M_123 ASD
Invalid : M_123#ASD
Invalid : 1_M# ADD
Valid : M_125ASD
Valid : M_125$ASD
I am trying as below :
[A-Za-z0-9_$]
Not working properly. I need to set both valid and invalid sets for a word.
Can i do a match with regular expression?
Upvotes: 1
Views: 237
Reputation: 627327
Your regex [A-Za-z0-9_$]
presents a character class that matches a single character that is either an ASCII letter or digit, or _
or $
symbols. If you use it with std::regex_match
, it would only match a whole string that consists of just one char like that since the pattern is anchored by default when used with that method. If you use it with an std::regex_search
, a string like ([_])
would pass, since the regex is not anchored and can find partial matches.
To match 0 or more chars, you need to add *
quantifier after your class. To match one or more chars, you need to add +
quantifier after your character class. However, you have an additional restriction: a digit cannot appear at the start.
It seems you may use
^[A-Za-z][A-Za-z0-9_$]*$
See the regex demo at regex101.com.
Details:
^
- start of string[A-Za-z]
- an ASCII letter (exactly one occurrence)[A-Za-z0-9_$]*
- 0+ ASCII letters, digits, _
or $
$
- end of string anchor.Note that with regex_match
, you may omit ^
and $
anchors.
Upvotes: 1
Reputation: 992
So the requirements are
cannot start with number( i am assuming it as start with alphabet)
cannot contain space or #
all other characters are valid
you can try this regex ^[a-zA-Z]((?![\# ]).)+?$
^[a-zA-Z]
checks for alphabet at start of the line
((?![\# ]).)+?$
checks if there are no #
or space
in the remaining part of the line.
Online demo here
EDIT
As per Wiktor's comment the regex can be simplified to ^[a-zA-Z][^# ]+$
.
Upvotes: 0