RegEx to match a string - this should be easy
-
Thursday, September 20, 2012 8:41 PM
I need to validate whether a string matches a 'whole word' within another string. For example:
Input string: LLR-110
Compare string: "LLR-108 LLR-109 LLR-110"
Test() in this case should return True. The problem I've encountered is that an input string like "LLR-1" also returns true...
Pattern is set to "[\s]?LLR-110[^0-9A-Z]" or"[\s]?LLR-1[^0-9A-Z]" and both return True.
I checked this with RegExr's test page http://gskinner.com/RegExr/ and it worked as expected - returning True, then False.
So... obviously either I have something set wrong or VBA's regular expression code isn't working as standard.
Does anyone have a suggestion to resolve this? Thanks in advance - Steve
All Replies
-
Friday, September 21, 2012 8:35 AMAnswerer
Pls tell us what is the condition that must be fulfilled.
From your pattern I assume the folloing
1.One whitespace or no white space.I am not sure why this is optional by ? character.
2.Fixed string LLR-110
3.A character which is neither numeric neither alphabet
For this 3rd point if the target string is at end then it may not show expected result.Because at end of string "Nothing" is present which is not equal to something.
[\s]?LLR-1[^0-9] this will not matchLLR-108 LLR-109 LLR-1 Best Regards,
Asadulla Javed, Kolkata
---------------------------------------------------------------------------------------------
Please do not forget to click “Vote as Helpful” if any post helps you and "Mark as Answer”if it solves the issue. -
Saturday, September 22, 2012 2:26 AM
On Thu, 20 Sep 2012 20:41:55 +0000, gawiz2009 wrote:>>>I need to validate whether a string matches a 'whole word' within another string. For example:>>Input string: LLR-110>>Compare string: "LLR-108 LLR-109 LLR-110">>Test() in this case should return True. The problem I've encountered is that an input string like "LLR-1" also returns true...>>Pattern is set to "[\s]?LLR-110[^0-9A-Z]" or"[\s]?LLR-1[^0-9A-Z]" and both return True.>>I checked this with RegExr's test page http://gskinner.com/RegExr/ and it worked as expected - returning True, then False.>>So... obviously either I have something set wrong or VBA's regular expression code isn't working as standard.>>Does anyone have a suggestion to resolve this? Thanks in advance - SteveI'm not entirely clear as to just what you want to do.If you want to determine if some string can be found, as a "whole word", in the compare string "LLR-108 LLR-109 LLR-110" , then you could use a regex such as:"\b(?:LLR-1(?:0[89]|10))\b"However, if your words might include characters that are other than digits, letters, or the underscore (in other words, the beginning/end of a word is a <space> or the beginning/end of the compare string, then:"(?:^|\s+)LLR-1(?:0[89]|10)(?:\s+|$)"If you need to capture the result, rather than just using the Test method, then you would need a capturing group for the second example, so as tgo avoid returning the <space>"(?:^|\s+)(LLR-1(?:0[89]|10))(?:\s+|$)"If that is not what you mean, please be more specific.
Ron- Proposed As Answer by Learning and LearningEditor Saturday, September 22, 2012 10:07 AM
- Marked As Answer by Learning and LearningEditor Saturday, October 13, 2012 8:29 AM

