Reputation: 33
So far I have this..
search_str = "-X made"
regEx.Pattern = "(\S*)[^\s*]"
regEx.IgnoreCase = True
regEx.Global = True
If regEx.test(search_str) Then
Set matches = regEx.Execute(search_str)
extractStr = matches(0).SubMatches(0)
end if
I want extractStr to have -X ( anything before the first space), but I am not getting it.
Upvotes: 3
Views: 1322
Reputation: 66364
There are a few issues with the pattern string "(\S*)[^\s*]"
\S
and [^\s]
are the same thing[foo*]
means match f
, o
or *
You probably want something like this
regEx.Pattern = "(\S+)(?=\s)"
Which means
(\S+)
match one or more non-whitespace (and remember the match) greedily until..(?=\s)
the next character is whitespacePlease note that a string like " "
will not match, if you want this to match too, use the zero-or-more *
in place of the one-or-more +
If you want to always match and you want to **always start from the beginning of the String, the RegExp becomes something like this
regEx.Pattern = "^(\S*)(?=\s|$)"
Where
^
matches the beginning of the stringfoo|$
matches foo
or the end of the stringUpvotes: 4