I need some help on Regex. I need to find a word that is surrounded by whatever element, for example - *
. But I need to match it only if it has spaces or nothing on the ether sides. For example if it is at start of the text 开发者_StackOverflowI can't really have space there, same for end.
Here is what I came up to
string myString = "You will find *me*, and *me* also!";
string findString = @"(\*(.*?)\*)";
string foundText;
MatchCollection matchCollection = Regex.Matches(myString, findString);
foreach (Match match in matchCollection)
{
foundText = match.Value.Replace("*", "");
myString = myString.Replace(match.Value, "->" + foundText + "<-");
match.NextMatch();
}
Console.WriteLine(myString);
You will find ->me<-, and ->me<- also!
Works correct, the problem is when I add *
in the middle of text, I don't want it to match then.
Example: You will find *m*e*, and *me* also!
You will find ->m<-e->, and <-me* also!
How can I fix that?
Try the following pattern:
string findString = @"(?<=\s|^)\*(.*?)\*(?=\s|$)";
(?<=\s|^)X
will match anyX
only if preceded by a space-char (\s
), or the start-of-input, andX(?=\s|$)
matches anyX
if followed by a space-char (\s
), or the end-of-input.
Note that it will not match *me*
in foo *me*, bar
since the second *
has a ,
after it! If you want to match that too, you need to include the comma like this:
string findString = @"(?<=[\s,]|^)\*(.*?)\*(?=[\s,]|$)";
You'll need to expand the set [\s,]
as you see fit, of course. You might want to add !
, ?
and .
at the very least: [\s,!?.]
(and no, .
and ?
do not need to be escaped inside a character-set!).
EDIT
A small demo:
string Txt = "foo *m*e*, bar";
string Pattern = @"(?<=[\s,]|^)\*(.*?)\*(?=[\s,]|$)";
Console.WriteLine(Regex.Replace(Txt, Pattern, ">$1<"));
which would print:
>m*e<
You can add "beginning of line or space" and "space or end of line" around your match:
(^|\s)\*(.*?)\*(\s|$)
You'll now need to refer to the middle capture group for the match string.
精彩评论