G
Guest
Hi All,
Being a bit of a newbie with regex, I am confused when using word boundaries.
For instance, I want to replace all the stand alone '.5k' that occur in an
input string, with 500. In other words
"this is a .5k example" goes to "this is a 500 example"
The replace should not touch '.5k' that occurs inside a word. For example:
"this 30.5k is not an example" should be unchanged.
So, I put together the regex below, thinking that the \b would match word
boundaries, and only replace stand alone occurences of '.5k'
Regex r = new Regex(@"\b\.5k\b");
MatchCollection mColl = r.Matches(txtInput.Text);
StringBuilder sb = new StringBuilder(txtInput.Text);
foreach (Match m in mColl)
{
sb.Remove(m.Index, 3);
sb.Insert(m.Index, "500");
}
txtResults.Text = sb.ToString();
(I used the stringbuilder Remove method, rather than the regex replace
method, since the documentation states that \b matches a backspace when used
in a replace operation.)
If the txtInput.Text is this
..5k is an example, not like this 30.5k but this .5k or this .5k
the txtResults.Text is this
..5k is an example, not like this 30500 but this .5k or this .5k
which is the complete opposite of what I would expect.
Also, if I replace the regex with this @"\B\.5k", the output from the above
code is
500 is an example, not like this 30.5k but this 500 or this 500
But doesn't \B mean that the match must not occur on a word boundary? (Is
not the change from a [space] to [5] a word boundary?)
I am more than willing to believe that the fault is my comprehension of this
stuff, but I am a bit stuck to see where I am going wrong at the moment.
So, any pointers as to where to look would be most appreciated. Many thanks
regards,
Gary
Being a bit of a newbie with regex, I am confused when using word boundaries.
For instance, I want to replace all the stand alone '.5k' that occur in an
input string, with 500. In other words
"this is a .5k example" goes to "this is a 500 example"
The replace should not touch '.5k' that occurs inside a word. For example:
"this 30.5k is not an example" should be unchanged.
So, I put together the regex below, thinking that the \b would match word
boundaries, and only replace stand alone occurences of '.5k'
Regex r = new Regex(@"\b\.5k\b");
MatchCollection mColl = r.Matches(txtInput.Text);
StringBuilder sb = new StringBuilder(txtInput.Text);
foreach (Match m in mColl)
{
sb.Remove(m.Index, 3);
sb.Insert(m.Index, "500");
}
txtResults.Text = sb.ToString();
(I used the stringbuilder Remove method, rather than the regex replace
method, since the documentation states that \b matches a backspace when used
in a replace operation.)
If the txtInput.Text is this
..5k is an example, not like this 30.5k but this .5k or this .5k
the txtResults.Text is this
..5k is an example, not like this 30500 but this .5k or this .5k
which is the complete opposite of what I would expect.
Also, if I replace the regex with this @"\B\.5k", the output from the above
code is
500 is an example, not like this 30.5k but this 500 or this 500
But doesn't \B mean that the match must not occur on a word boundary? (Is
not the change from a [space] to [5] a word boundary?)
I am more than willing to believe that the fault is my comprehension of this
stuff, but I am a bit stuck to see where I am going wrong at the moment.
So, any pointers as to where to look would be most appreciated. Many thanks
regards,
Gary