Can I use regular expressions with String.Replace in C#?
up vote
13
down vote
favorite
For example I have code below
string txt="I have strings like West, and West; and west, and Western."
I would like to replace the word west or West with some other word. But I would like not to replace West in Western.
- Can I use regular expression in string.replace? I used
inputText.Replace("(\sWest.\s)",temp);
It dos not work.
c# string text matching
add a comment |
up vote
13
down vote
favorite
For example I have code below
string txt="I have strings like West, and West; and west, and Western."
I would like to replace the word west or West with some other word. But I would like not to replace West in Western.
- Can I use regular expression in string.replace? I used
inputText.Replace("(\sWest.\s)",temp);
It dos not work.
c# string text matching
add a comment |
up vote
13
down vote
favorite
up vote
13
down vote
favorite
For example I have code below
string txt="I have strings like West, and West; and west, and Western."
I would like to replace the word west or West with some other word. But I would like not to replace West in Western.
- Can I use regular expression in string.replace? I used
inputText.Replace("(\sWest.\s)",temp);
It dos not work.
c# string text matching
For example I have code below
string txt="I have strings like West, and West; and west, and Western."
I would like to replace the word west or West with some other word. But I would like not to replace West in Western.
- Can I use regular expression in string.replace? I used
inputText.Replace("(\sWest.\s)",temp);
It dos not work.
c# string text matching
c# string text matching
edited May 5 '10 at 6:44
Roger Lipscombe
55.2k42185309
55.2k42185309
asked May 5 '10 at 6:30
Tasawer Khan
3,37463464
3,37463464
add a comment |
add a comment |
9 Answers
9
active
oldest
votes
up vote
20
down vote
accepted
To replace the whole word (rather than part of the word):
string s = Regex.Replace(s, @"bwestb", "something");
Looks alright but this will ignore west, and west. And is it case insensitive?
– Tasawer Khan
May 5 '10 at 6:44
I think it does the same as I am already doing Using 's=s.Replace(" West ","something");'
– Tasawer Khan
May 5 '10 at 6:50
It works like string s = Regex.Replace(s, @"(bwestb)", "something");. And it works for west. and west, and west; as well. Dont really understand why :)
– Tasawer Khan
May 5 '10 at 7:06
The "b" matches a "word boundary". This regex is case sensitive, but you can add a RegexOptions.IgnoreCase (4th param) to make it case insensitive.
– Hans Kesting
May 6 '10 at 7:27
add a comment |
up vote
23
down vote
Answer to the question is NO - you cannot use regexp in string.Replace.
If you want to use a regular expression, you must use the Regex class, as everyone stated in their answers.
4
+1 only answer that directly answers the question if you can use String.Replace also for regex expressions
– drkthng
Oct 6 '15 at 9:37
add a comment |
up vote
7
down vote
Have you looked at Regex.Replace
? Also, be sure to catch the return value; Replace
(via any string mechanism) returns a new string - it doesn't do an in-place replace.
add a comment |
up vote
4
down vote
Try using the System.Text.RegularExpressions.Regex
class. It has a static Replace
method. I'm not good with regular expressions, but something like
string outputText = Regex.Replace(inputText, "(\sWest.\s)", temp);
should work, if your regular expression is correct.
add a comment |
up vote
2
down vote
USe this code if you want it to be case insensitive
string pattern = @"bwestb";
string modifiedString = Regex.Replace(input, pattern, strReplacement, RegexOptions.IgnoreCase);
add a comment |
up vote
1
down vote
I agree with Robert Harvey's solution except for one small modification:
s = Regex.Replace(s, @"bwestb", "something", RegexOptions.IgnoreCase);
This will replace both "West" and "west" with your new word
add a comment |
up vote
0
down vote
Insert the regular expression in the code before class
using System.Text.RegularExpressions;
below is the code for string replace using regex
string input = "Dot > Not Perls";
// Use Regex.Replace to replace the pattern in the input.
string output = Regex.Replace(input, "some string", ">");
source : http://www.dotnetperls.com/regex-replace
add a comment |
up vote
0
down vote
In Java, String#replace
accepts strings in regex format but C# can do this as well using extensions:
public static string ReplaceX(this string text, string regex, string replacement) {
return Regex.Replace(text, regex, replacement);
}
And use it like:
var text = " space more spaces ";
text.Trim().ReplaceX(@"s+", " "); // "space more spaces"
add a comment |
up vote
-1
down vote
class Regex is static, when You use the method Replace (Regex.Replace).
public static class Extension
{
public static string ReplaceValue(string data,string criteria)
{
return s = Regex.Replace(s, @"bwestb", "something");
}
}
1
This looks like a question. Ask as a question. Also, you posted this non-answer to a question asked, and answered, 6 years ago.
– David Makogon
Jul 21 '16 at 22:10
add a comment |
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
20
down vote
accepted
To replace the whole word (rather than part of the word):
string s = Regex.Replace(s, @"bwestb", "something");
Looks alright but this will ignore west, and west. And is it case insensitive?
– Tasawer Khan
May 5 '10 at 6:44
I think it does the same as I am already doing Using 's=s.Replace(" West ","something");'
– Tasawer Khan
May 5 '10 at 6:50
It works like string s = Regex.Replace(s, @"(bwestb)", "something");. And it works for west. and west, and west; as well. Dont really understand why :)
– Tasawer Khan
May 5 '10 at 7:06
The "b" matches a "word boundary". This regex is case sensitive, but you can add a RegexOptions.IgnoreCase (4th param) to make it case insensitive.
– Hans Kesting
May 6 '10 at 7:27
add a comment |
up vote
20
down vote
accepted
To replace the whole word (rather than part of the word):
string s = Regex.Replace(s, @"bwestb", "something");
Looks alright but this will ignore west, and west. And is it case insensitive?
– Tasawer Khan
May 5 '10 at 6:44
I think it does the same as I am already doing Using 's=s.Replace(" West ","something");'
– Tasawer Khan
May 5 '10 at 6:50
It works like string s = Regex.Replace(s, @"(bwestb)", "something");. And it works for west. and west, and west; as well. Dont really understand why :)
– Tasawer Khan
May 5 '10 at 7:06
The "b" matches a "word boundary". This regex is case sensitive, but you can add a RegexOptions.IgnoreCase (4th param) to make it case insensitive.
– Hans Kesting
May 6 '10 at 7:27
add a comment |
up vote
20
down vote
accepted
up vote
20
down vote
accepted
To replace the whole word (rather than part of the word):
string s = Regex.Replace(s, @"bwestb", "something");
To replace the whole word (rather than part of the word):
string s = Regex.Replace(s, @"bwestb", "something");
answered May 5 '10 at 6:35
Robert Harvey♦
147k33270413
147k33270413
Looks alright but this will ignore west, and west. And is it case insensitive?
– Tasawer Khan
May 5 '10 at 6:44
I think it does the same as I am already doing Using 's=s.Replace(" West ","something");'
– Tasawer Khan
May 5 '10 at 6:50
It works like string s = Regex.Replace(s, @"(bwestb)", "something");. And it works for west. and west, and west; as well. Dont really understand why :)
– Tasawer Khan
May 5 '10 at 7:06
The "b" matches a "word boundary". This regex is case sensitive, but you can add a RegexOptions.IgnoreCase (4th param) to make it case insensitive.
– Hans Kesting
May 6 '10 at 7:27
add a comment |
Looks alright but this will ignore west, and west. And is it case insensitive?
– Tasawer Khan
May 5 '10 at 6:44
I think it does the same as I am already doing Using 's=s.Replace(" West ","something");'
– Tasawer Khan
May 5 '10 at 6:50
It works like string s = Regex.Replace(s, @"(bwestb)", "something");. And it works for west. and west, and west; as well. Dont really understand why :)
– Tasawer Khan
May 5 '10 at 7:06
The "b" matches a "word boundary". This regex is case sensitive, but you can add a RegexOptions.IgnoreCase (4th param) to make it case insensitive.
– Hans Kesting
May 6 '10 at 7:27
Looks alright but this will ignore west, and west. And is it case insensitive?
– Tasawer Khan
May 5 '10 at 6:44
Looks alright but this will ignore west, and west. And is it case insensitive?
– Tasawer Khan
May 5 '10 at 6:44
I think it does the same as I am already doing Using 's=s.Replace(" West ","something");'
– Tasawer Khan
May 5 '10 at 6:50
I think it does the same as I am already doing Using 's=s.Replace(" West ","something");'
– Tasawer Khan
May 5 '10 at 6:50
It works like string s = Regex.Replace(s, @"(bwestb)", "something");. And it works for west. and west, and west; as well. Dont really understand why :)
– Tasawer Khan
May 5 '10 at 7:06
It works like string s = Regex.Replace(s, @"(bwestb)", "something");. And it works for west. and west, and west; as well. Dont really understand why :)
– Tasawer Khan
May 5 '10 at 7:06
The "b" matches a "word boundary". This regex is case sensitive, but you can add a RegexOptions.IgnoreCase (4th param) to make it case insensitive.
– Hans Kesting
May 6 '10 at 7:27
The "b" matches a "word boundary". This regex is case sensitive, but you can add a RegexOptions.IgnoreCase (4th param) to make it case insensitive.
– Hans Kesting
May 6 '10 at 7:27
add a comment |
up vote
23
down vote
Answer to the question is NO - you cannot use regexp in string.Replace.
If you want to use a regular expression, you must use the Regex class, as everyone stated in their answers.
4
+1 only answer that directly answers the question if you can use String.Replace also for regex expressions
– drkthng
Oct 6 '15 at 9:37
add a comment |
up vote
23
down vote
Answer to the question is NO - you cannot use regexp in string.Replace.
If you want to use a regular expression, you must use the Regex class, as everyone stated in their answers.
4
+1 only answer that directly answers the question if you can use String.Replace also for regex expressions
– drkthng
Oct 6 '15 at 9:37
add a comment |
up vote
23
down vote
up vote
23
down vote
Answer to the question is NO - you cannot use regexp in string.Replace.
If you want to use a regular expression, you must use the Regex class, as everyone stated in their answers.
Answer to the question is NO - you cannot use regexp in string.Replace.
If you want to use a regular expression, you must use the Regex class, as everyone stated in their answers.
answered Jan 21 '15 at 9:44
dortique
50049
50049
4
+1 only answer that directly answers the question if you can use String.Replace also for regex expressions
– drkthng
Oct 6 '15 at 9:37
add a comment |
4
+1 only answer that directly answers the question if you can use String.Replace also for regex expressions
– drkthng
Oct 6 '15 at 9:37
4
4
+1 only answer that directly answers the question if you can use String.Replace also for regex expressions
– drkthng
Oct 6 '15 at 9:37
+1 only answer that directly answers the question if you can use String.Replace also for regex expressions
– drkthng
Oct 6 '15 at 9:37
add a comment |
up vote
7
down vote
Have you looked at Regex.Replace
? Also, be sure to catch the return value; Replace
(via any string mechanism) returns a new string - it doesn't do an in-place replace.
add a comment |
up vote
7
down vote
Have you looked at Regex.Replace
? Also, be sure to catch the return value; Replace
(via any string mechanism) returns a new string - it doesn't do an in-place replace.
add a comment |
up vote
7
down vote
up vote
7
down vote
Have you looked at Regex.Replace
? Also, be sure to catch the return value; Replace
(via any string mechanism) returns a new string - it doesn't do an in-place replace.
Have you looked at Regex.Replace
? Also, be sure to catch the return value; Replace
(via any string mechanism) returns a new string - it doesn't do an in-place replace.
answered May 5 '10 at 6:36
Marc Gravell♦
770k19021172529
770k19021172529
add a comment |
add a comment |
up vote
4
down vote
Try using the System.Text.RegularExpressions.Regex
class. It has a static Replace
method. I'm not good with regular expressions, but something like
string outputText = Regex.Replace(inputText, "(\sWest.\s)", temp);
should work, if your regular expression is correct.
add a comment |
up vote
4
down vote
Try using the System.Text.RegularExpressions.Regex
class. It has a static Replace
method. I'm not good with regular expressions, but something like
string outputText = Regex.Replace(inputText, "(\sWest.\s)", temp);
should work, if your regular expression is correct.
add a comment |
up vote
4
down vote
up vote
4
down vote
Try using the System.Text.RegularExpressions.Regex
class. It has a static Replace
method. I'm not good with regular expressions, but something like
string outputText = Regex.Replace(inputText, "(\sWest.\s)", temp);
should work, if your regular expression is correct.
Try using the System.Text.RegularExpressions.Regex
class. It has a static Replace
method. I'm not good with regular expressions, but something like
string outputText = Regex.Replace(inputText, "(\sWest.\s)", temp);
should work, if your regular expression is correct.
answered May 5 '10 at 6:40
Peter
1,3721118
1,3721118
add a comment |
add a comment |
up vote
2
down vote
USe this code if you want it to be case insensitive
string pattern = @"bwestb";
string modifiedString = Regex.Replace(input, pattern, strReplacement, RegexOptions.IgnoreCase);
add a comment |
up vote
2
down vote
USe this code if you want it to be case insensitive
string pattern = @"bwestb";
string modifiedString = Regex.Replace(input, pattern, strReplacement, RegexOptions.IgnoreCase);
add a comment |
up vote
2
down vote
up vote
2
down vote
USe this code if you want it to be case insensitive
string pattern = @"bwestb";
string modifiedString = Regex.Replace(input, pattern, strReplacement, RegexOptions.IgnoreCase);
USe this code if you want it to be case insensitive
string pattern = @"bwestb";
string modifiedString = Regex.Replace(input, pattern, strReplacement, RegexOptions.IgnoreCase);
edited May 5 '10 at 6:57
answered May 5 '10 at 6:50
Archie
1,30683150
1,30683150
add a comment |
add a comment |
up vote
1
down vote
I agree with Robert Harvey's solution except for one small modification:
s = Regex.Replace(s, @"bwestb", "something", RegexOptions.IgnoreCase);
This will replace both "West" and "west" with your new word
add a comment |
up vote
1
down vote
I agree with Robert Harvey's solution except for one small modification:
s = Regex.Replace(s, @"bwestb", "something", RegexOptions.IgnoreCase);
This will replace both "West" and "west" with your new word
add a comment |
up vote
1
down vote
up vote
1
down vote
I agree with Robert Harvey's solution except for one small modification:
s = Regex.Replace(s, @"bwestb", "something", RegexOptions.IgnoreCase);
This will replace both "West" and "west" with your new word
I agree with Robert Harvey's solution except for one small modification:
s = Regex.Replace(s, @"bwestb", "something", RegexOptions.IgnoreCase);
This will replace both "West" and "west" with your new word
answered May 5 '10 at 7:23
Kyllan
63210
63210
add a comment |
add a comment |
up vote
0
down vote
Insert the regular expression in the code before class
using System.Text.RegularExpressions;
below is the code for string replace using regex
string input = "Dot > Not Perls";
// Use Regex.Replace to replace the pattern in the input.
string output = Regex.Replace(input, "some string", ">");
source : http://www.dotnetperls.com/regex-replace
add a comment |
up vote
0
down vote
Insert the regular expression in the code before class
using System.Text.RegularExpressions;
below is the code for string replace using regex
string input = "Dot > Not Perls";
// Use Regex.Replace to replace the pattern in the input.
string output = Regex.Replace(input, "some string", ">");
source : http://www.dotnetperls.com/regex-replace
add a comment |
up vote
0
down vote
up vote
0
down vote
Insert the regular expression in the code before class
using System.Text.RegularExpressions;
below is the code for string replace using regex
string input = "Dot > Not Perls";
// Use Regex.Replace to replace the pattern in the input.
string output = Regex.Replace(input, "some string", ">");
source : http://www.dotnetperls.com/regex-replace
Insert the regular expression in the code before class
using System.Text.RegularExpressions;
below is the code for string replace using regex
string input = "Dot > Not Perls";
// Use Regex.Replace to replace the pattern in the input.
string output = Regex.Replace(input, "some string", ">");
source : http://www.dotnetperls.com/regex-replace
answered Oct 1 '14 at 13:01
Talha
35
35
add a comment |
add a comment |
up vote
0
down vote
In Java, String#replace
accepts strings in regex format but C# can do this as well using extensions:
public static string ReplaceX(this string text, string regex, string replacement) {
return Regex.Replace(text, regex, replacement);
}
And use it like:
var text = " space more spaces ";
text.Trim().ReplaceX(@"s+", " "); // "space more spaces"
add a comment |
up vote
0
down vote
In Java, String#replace
accepts strings in regex format but C# can do this as well using extensions:
public static string ReplaceX(this string text, string regex, string replacement) {
return Regex.Replace(text, regex, replacement);
}
And use it like:
var text = " space more spaces ";
text.Trim().ReplaceX(@"s+", " "); // "space more spaces"
add a comment |
up vote
0
down vote
up vote
0
down vote
In Java, String#replace
accepts strings in regex format but C# can do this as well using extensions:
public static string ReplaceX(this string text, string regex, string replacement) {
return Regex.Replace(text, regex, replacement);
}
And use it like:
var text = " space more spaces ";
text.Trim().ReplaceX(@"s+", " "); // "space more spaces"
In Java, String#replace
accepts strings in regex format but C# can do this as well using extensions:
public static string ReplaceX(this string text, string regex, string replacement) {
return Regex.Replace(text, regex, replacement);
}
And use it like:
var text = " space more spaces ";
text.Trim().ReplaceX(@"s+", " "); // "space more spaces"
answered May 17 '17 at 6:21
mr5
1,55522546
1,55522546
add a comment |
add a comment |
up vote
-1
down vote
class Regex is static, when You use the method Replace (Regex.Replace).
public static class Extension
{
public static string ReplaceValue(string data,string criteria)
{
return s = Regex.Replace(s, @"bwestb", "something");
}
}
1
This looks like a question. Ask as a question. Also, you posted this non-answer to a question asked, and answered, 6 years ago.
– David Makogon
Jul 21 '16 at 22:10
add a comment |
up vote
-1
down vote
class Regex is static, when You use the method Replace (Regex.Replace).
public static class Extension
{
public static string ReplaceValue(string data,string criteria)
{
return s = Regex.Replace(s, @"bwestb", "something");
}
}
1
This looks like a question. Ask as a question. Also, you posted this non-answer to a question asked, and answered, 6 years ago.
– David Makogon
Jul 21 '16 at 22:10
add a comment |
up vote
-1
down vote
up vote
-1
down vote
class Regex is static, when You use the method Replace (Regex.Replace).
public static class Extension
{
public static string ReplaceValue(string data,string criteria)
{
return s = Regex.Replace(s, @"bwestb", "something");
}
}
class Regex is static, when You use the method Replace (Regex.Replace).
public static class Extension
{
public static string ReplaceValue(string data,string criteria)
{
return s = Regex.Replace(s, @"bwestb", "something");
}
}
edited Jul 21 '16 at 22:24
answered Jul 21 '16 at 21:46
slnit
185
185
1
This looks like a question. Ask as a question. Also, you posted this non-answer to a question asked, and answered, 6 years ago.
– David Makogon
Jul 21 '16 at 22:10
add a comment |
1
This looks like a question. Ask as a question. Also, you posted this non-answer to a question asked, and answered, 6 years ago.
– David Makogon
Jul 21 '16 at 22:10
1
1
This looks like a question. Ask as a question. Also, you posted this non-answer to a question asked, and answered, 6 years ago.
– David Makogon
Jul 21 '16 at 22:10
This looks like a question. Ask as a question. Also, you posted this non-answer to a question asked, and answered, 6 years ago.
– David Makogon
Jul 21 '16 at 22:10
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2771042%2fcan-i-use-regular-expressions-with-string-replace-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown