tags:

views:

26

answers:

1

I do Regular Expressions so rarely that they always challenge me. Even simple ones.

How can I make a regular expression that will match all of these:

 := 'abc'
 := 'xyz'
 := '2rs'
 := 'abe'
 := 'a2c'

Basically it starts with := ' and ends with ' and has three values inside. Could be numbers or chars.

+2  A: 

Something like this should work (as seen on rubular.com):

:= '([a-z0-9]{3})'

Explanation:

  • := ' is matched literally since they're not metacharacters
  • [a-z0-9] defines a character class matching lowercase letters and digits
  • {3} is exact repetition, 3 times
  • (...) is a capturing group (not needed, but probably handy)

Minor variations on this pattern include:

  • [a-zA-Z0-9] instead to also allow uppercase letters
  • {1,3} instead to allow between 1-3 repetition
  • := *' instead to allow any number of space (* here means "zero or more repetition of")

regular-expressions.info

polygenelubricants
That looks great! Thanks. However, I must have an old Reg Ex engine cause it did not work. (I am using GExperts grep with Delphi 5 (sad I know)). I may try to find a more up-to-date tool to search in files. Thanks for the additional info and links.
Vaccano
@Vaccano: I don't have any info on how GExperts `grep` works, but I believe in GNU `grep` repetition is `\{3\}` (http://www.regular-expressions.info/gnu.html#bre); go ahead and try that and tell me if it works. (Also, this is why you should always specify flavor when asking regex question).
polygenelubricants
Alas no. I took the whole repetition part out and it did not match anything. Thanks for the help, your answer is right and I will mark it as such. I will then try to find a more current reg ex engine to search my files.
Vaccano
Similarly, some older GNU stuff uses `\\(`..`\\)` for grouping, with non-escaped ones being literals, so try adding a backslash (or removing the group) and see if that helps.
Peter Boughton
I'd also ditch the $ incase you have trailing whitespace. (Or use `\s*` also.)
Peter Boughton
@Peter Boughton - I think there are several things wrong (with my grep tool). First it says `Character immediately following: at 3 is not a valid grep search criteria.` I remove the `:` and just search with `^ = '\\([a-z0-9]\{3\}\\)'` and I get no results. My tool was new in the 90s so I am guessing it is just not used to current syntax.
Vaccano
If you know a nice tool to search in files with Regular Expressions, please post it here: http://superuser.com/questions/161797/search-files-with-regular-expressions
Vaccano
@Vaccano: try `:= '[a-z0-9][a-z0-9][a-z0-9]'`
polygenelubricants
I tried that and it did not work. Something is very very wrong with this tool search. I tried `[a-z0-9]` and got no results. That should have returned a ton of results. (However doing `[:= ']` does return all the instance of `:=`.)
Vaccano
I checked the help file (I know I should have looked at that first) It says `The GExperts regular expression engine does not support the '*' modifier (which usually matches 0 or more copies of the previous character or block) or character groups using brackets '{}'.` It says it supports \, ^, $, ., :, and []. I will work with that and see what I can get.
Vaccano
@Vaccano: I've edited my answer to remove the anchors, which I don't think you actually need anyway. In any case, `[a-z]` is what is called a "character class"; `-` here is a "range", so look for terms that sound similar to these. `[a-z]` by itself matches one and exactly one character, if that character is one of `a`, `b`, `c`, ... `z`
polygenelubricants
Thanks for all the help. I think that GExperts "rolled there own" engine. Turns out this is what I needed: `\:= ':a:a:a'`
Vaccano
@Vaccano: don't hesitate on writing your own answer about how to do this in GExperts based on your research. I'm sure others in the future will appreciate your effort. You can put it in a new question too if you want (e.g. "how does regex syntax in gexperts work?" etc). It's okay to answer your own question in stackoverflow as long as it's good content.
polygenelubricants