views:

92

answers:

3

I never use regular expression before and plan to use it to solve my problem but not quite sure whether it can help me.

I have a situation where I need store a rule or formula to build string values like following examples in a database field then retrieve this rule and build the string value.

  • FacilityCode + Left(ModelNO,2)
  • Right(PO,3) + Left(Serial,2)

Is this achievable using .net regular expression? Any good tutorial or simple examples of this problem.

+2  A: 

Regexp : http://msdn.microsoft.com/en-us/library/2k3te2cs(VS.80).aspx

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx

But it doesn't seems fitting :)

ykatchou
Probably a loop for finding words and a switch statement will be as efficient, take a look to http://en.wikipedia.org/wiki/Polish_notation
ykatchou
I agree, regex expression not good choice for this. I'm thinking to build own parser to build and read the formula. It going to be simple parser that provide basic string functionality.
BaLs
+2  A: 

It might be better to code some random string generator. Regex is for searching data not creating data.

The thing to remember about regex is that it is like an aircraft carrier; it does one thing very very well, it does not do other jobs very well at all.

An aircraft carrier moves planes very well on the ocean; it does not make a cheese sandwich well AT ALL!!

That is to say, if you use regex when you shouldn't you will almost certainly use far more processing power than if you used another tool for that job. Html parsing comes to mind.

Keng
+1  A: 

Regex is provided as part of System.Text.RegularExpressions, but you can't rely exclusively on it. It'll let you search existing strings, but you'll need to implement your own logic for building new strings based on what you find in the existing data.

Also, keep in mind that System.Text.RegularExpressions works differently from regexp in Perl and other implementations. For example, it doesn't recognize POSIX character class definitions.

Since you're new to regex, you might want to check out the "Regular Expressions User Guide" on zytrax.com. It's not as comprehensive as an O'Reilly manual, but it'll do as a start.

Matthew Graybosch