(*) regex is available in <regex> header file
(*) It's used for pattern match
(*) [a-z] - means any one char from a to z e.g. a , b, r
(*) [ ] - parenthesis are used for a group.
(**) \\ - to use any special character in regular expression like " or [ etc
(*) [a-z]+ - Any one or more characters from a to z. e.g. aa, axghdk, dgholgh
+ means "Match the expression one or more times in addition to matching it once."
(*) abc+ - means abc plus any number of times c. e.g abccccc, abc, but not aabc
(*) [abc]+ or (abc)+ - any number of times abc e.g. abcabcabc
(*) [a-z]+ - In the range a to z, repeat any number of times. a, aa, tehte, hfod
(*) [a-z]* - Same as above but includes empty string also e.g. , aa, dklgkj
(*) [a-zA-Z0-9] - Matches single lowercase letter, uppercase letter, or digit
(*) abc8 is also one of the valid regular expression which will exactly match with same
(*) [\\[0-9] - A char either [ or number. notice \\(double escape char used before [
without \\, [ has special meaning.
(*) c[a-z]*t - first letter c, last letter t. In middle any number of times a to z.
e.g. caaabbt, cttt, ct,
(*) c[a-su-z]t - Same as above in middle there shouldn't be t.
(*) One example asked in tally. Match with pattern "abc=def; xyz=lmn"
1. start and end there should be doulbe quotes
2. key and value pair should be separated by ;(semicolon), except the last pair.
3. between two key-pair you can have space or without space. "abc=def; xyz=lmn" or "abc=def;xyz=lmn"
4. kay an value should be spearated by = without space
Let's start:
Step 1: we want start and end double quotes
regex rx("\"[a-zA-Z0-9]+\"");
// in middle there could be anything but start and
end should be "(doulbe quotes). e.g "sfs" , "389hkjds"
Step 2: key value should be separated by equal
regex rx("\"[a-zA-Z0-9]+\\=[a-zA-Z0-9]+\"");
//Repeat the same patter in left and right and = in middle
Step 3: It should be ending with ;
"\"[a-zA-Z0-9]+\\=[a-zA-Z0-9]+\\;\""
// this is correct but doesn't respect if
the last pair shouldn't have ;(secolon).
Step 4: It should repeat this for any number of pairs
"\"([a-zA-Z0-9]+\\=[a-zA-Z0-9]+\\;)+\""
//Put whole thing in bracket and add +
Step 5: It should be ending with ; but not for last pair.
"\"([a-zA-Z0-9]+\\=[a-zA-Z0-9]+\\;)+([a-zA-Z0-9]+\\=[a-zA-Z0-9]+\\)\""
//THis is interesting. copied the whole thing without ; at end. seond pair
//is not repeating
Step 6: The first part can be there or not; eg. "abc=def". it's valid to have
without semicolon.
"\"[[a-zA-Z0-9]+\\=[a-zA-Z0-9]+\\;]*([a-zA-Z0-9]+\\=[a-zA-Z0-9]+\\)\""
//Changed the first part to []*. Means it can be null also
Step 7: There can be space between two pairs after ;. "abc=def; lmn=pqr"
"\"[[a-zA-Z0-9]+\\=[a-zA-Z0-9]+\\;[ ]*]*([a-zA-Z0-9]+\\=[a-zA-Z0-9]+\\)\""
//Added [ ]* in first part at end. 0 or more space.
This is the final solution.
a) there are two parts, first part can be null also
b) Only in beginning and end it has "
c) First part should be ending with ;(semicolon)
d) There can be space after secolon
e) Each part can have one or more combination of character separated by
=.
f) The second part is not repeating.