C # regex matches only parts of full words in a string
Before asking this question, I have Googled for this issue and have looked at all StackOverflow related questions.
The problem is pretty simple
I have a line "North Atlantic Treaty Organization"
I have a pattern "a. * Z" when it will match
northern organization ATLATIC TREATY
But I need it to only match full words (like orgANIZation)
I tried "\ baz \ b" and "\ Baz \ B" as a pattern, but I think I didn't quite understand it
How do I change my pattern to match full words containing a string (without multiple words matching)
The templates are generated on the fly, the user enters * z, and my application translates it into a template that matches parts of full words in a string.
My problem is that I don't know what the user will be looking for. Ideally, I would prefer a regex expression to the user.
Thank!
source to share
ANIZ in orgANIZation is not a complete word - it is part of a word. Your btw template - not what you wrote - a*z
doesn't match your description; you are probably using a.*z
which is what it means. So, try a[^ ]*z
so that it doesn't match whitespace. If there are other characters other than spaces that you don't want to match, eg. some types of punctuation, of course, insert them into the construction [^...]
.
source to share
"a[^\s]*z"
This means "a" followed by any number of non-white characters, followed by "z".
EDIT: It seems to you that ' *
' is being interpreted as a wildcard. This way, the user does not have to enter a regex, but a string with certain wildcards. You can translate these wildcards into a regular expression by reasoning about the intended meaning. Let's say that ' *
' should mean "zero or more characters that are not spaces". You replace that character, then with a matching regex:
[^ \ s] *
`-.- ´ |
Character class ----- ´ '--- Zero or more of these
'\ s': "Whitespace"
Inside Character class: if it starts with '^': "not"
You can also define '?' as matching exactly one character without spaces. This is the same character class, but you omit the '*' at the end.
So what you are doing is regex, replace " *
" with " [^\s]*
" and " ?
" with " [^\s]
".
source to share
Not directly related to your question, but you can check out the RegEx renderer which shows the results obtained based on text input and a given regexp.
Such a tool is very useful for finding the right template, which can be quite complex. A good tool specialized for .net RegEx, RegExLab is a little older, but does a pretty good job of showing what exactly your regex matches. Since the page is in German, just click the link regexlab.006.zip. The source code is also included.
source to share
Regex reWord = new Regex("\\b[A-Za-z]*?(a.*z)[A-Za-z]*\\b");
... this will bring back the "Atlantic Treaty Organization", with a takeover from a. *
z is an "antique organization".
The problem is inherent in your method - unless you parsed the custom "regex" *
z (or a. *
Z, which is not entirely clear from your post), changing *
to [^ \ s] *
? as Svante suggests (or maybe \ w *
?), you are going to be collecting a lot more characters than you like.
". *
" is generally a bad idea when you're trying to be specific. This will match everything except a newline, and you can't add anything to it to stop that.
Regex reWord = new Regex("\\b\\w*?(a\\w*?z)\\w*\\b");
... only "Organization" will return.
Alternatively, if you are absolutely required to avoid modifying the custom regex for any reason, perhaps try to plan your strings into an array of words and test each word separately from the regex.
Ultimately, this is GIGO - trash, trash. Feed the system a bad regex, and if you don't fix it appropriately, you won't get what you're looking for.
source to share