regex - TCL regexp example -
i want word in string starts abc_ or xyz_ writing regexp. here script:
[regexp -nocase -- {.*\s+(abc_|xyz_\s+)\s+.*} $str necessarystr]
so if apply above written regexp on str1 , str2 want "xyz_hello" $str1 , "abc_bye" $str2.
set str1 "gfrdgasjklh dlasd =-0-489 xyz_hello sddf 89rn sf n9" set str2 "dytfasjklh abc_bye dlasd =-0tyj-489 sddf tyj89rn sjf n9"
but regexps not work. , questions are:
1) wrong regexp? 2) find works starting predefined prefixes regexp or better use string functions (string match or so)?
it not clear in question consitutes word. further underscores permitted? digits permitted? "words consist of prefix", e.g. "abc_" or "xyz"?
making conservative assumptions (based on examples) expecting letters english alphabet, @ least 1 further character, , don't care case, can simplify regexp:
[regexp -nocase -- {\m(abc_|xyz_)[a-za-z]+} $str match]
this set match
matching word. can replace conents of square brackets if definition of word differs assumptions.
your second question whether prefer regexp string functions depend upon context, , lead subjective territory.
some things consider:
- does performance matter? unless doing search in tight loop, or searching long strings, suspect performance difference not relevant. wait until have performance issue, profile application see bottleneck is, can test alternative implementations.
- convenience going depend upon preference of programmer(s) have write , maintain code. love/hate using regexps?
- using regexp offer more flexibility, can @ cost of readability.
my recommendation use whichever comfortable with. write set of unit tests code, optimise later if have identified bottleneck there during profiling.
Comments
Post a Comment