Fairly simple Regex question...
-
Hi there. I'm getting a headache finding a regular expression to determine if the char that I've just read in is a word char (\w) or a valid form of punctuation (not sure how to best describe this in regex terms). Essentailly I'm in a loop reading char by char and wanting to go do "something else" at the end of each word - unless the word is follwed by a semicolon or a comma or so-on. In this case I want to loop agin to include the punctuation char in the word and then go off and do the "something else". I have the "word char" bit so far: Regex reg = new Regex("\\w", RegexOptions.IgnoreCase|RegexOptions.Compiled); But I need to add the "or a punctuation char" bit to this. I tried to using a number constructs to add the but nothing is working out! Any help is greatly appreciated. Kind regards, John. _______ Your system must be restarted for these changes to take effect. This is not a drill.
-
Hi there. I'm getting a headache finding a regular expression to determine if the char that I've just read in is a word char (\w) or a valid form of punctuation (not sure how to best describe this in regex terms). Essentailly I'm in a loop reading char by char and wanting to go do "something else" at the end of each word - unless the word is follwed by a semicolon or a comma or so-on. In this case I want to loop agin to include the punctuation char in the word and then go off and do the "something else". I have the "word char" bit so far: Regex reg = new Regex("\\w", RegexOptions.IgnoreCase|RegexOptions.Compiled); But I need to add the "or a punctuation char" bit to this. I tried to using a number constructs to add the but nothing is working out! Any help is greatly appreciated. Kind regards, John. _______ Your system must be restarted for these changes to take effect. This is not a drill.
Use the OR operator (|) in your regular expression: \w|\.|:|;|\? If you haven't already, you may want to get a copy of Regex Coach. It's a free utility for designing and testing regular expressions.
Jim Conigliaro jconigliaro@ieee.org
http://www.jimconigliaro.com -
Use the OR operator (|) in your regular expression: \w|\.|:|;|\? If you haven't already, you may want to get a copy of Regex Coach. It's a free utility for designing and testing regular expressions.
Jim Conigliaro jconigliaro@ieee.org
http://www.jimconigliaro.comJim, That worked a treat - many thanks. I wasn't sure about the syntax - most of the web pages I saw have very complex examples or only code for one specifi thing - not a combination of several. Once again, thanks for your response. John. PS: The smiley in the middle of the expression was interesting!
-
Jim, That worked a treat - many thanks. I wasn't sure about the syntax - most of the web pages I saw have very complex examples or only code for one specifi thing - not a combination of several. Once again, thanks for your response. John. PS: The smiley in the middle of the expression was interesting!
Most examples are more complex because they use regular expression to match the entire strings, not just to identify single characters. You should consider using the regular expression for what you are doing, instead of looping through the characters.
--- b { font-weight: normal; }