Replace '$' using Regex - c#

I want to replace
! = change
# = static(does not)
$ = Want to replace
I got a string like this #!$!
How do I replace the $ with something else?
EDIT: I need to use Regex as the string may appear anywhere!

You don't need a regular expression, just use the String.Replace method:
String result = input.Replace("$", "somethingElse");
As a side note: The way that you would do this with a regular expression would be like this:
String result = Regex.Replace(input, #"\$", "somethingElse");
Notice that I have escaped the $ with a backslash since $ usually means match the end of the string.

Take a look at System.Text.RegularExpressions.Regex.Replace method.
Regex.Replace("#!$!", "!(.*)!", "replacement value");

Why do you want some RegExp for string replacement. You can just use string.Replace() fundtion.

also, check out Rubular, a great RegEx Tester.

Using the String class' .Replace() method would do the trick but, if you really want to use RegEx, this is a great RegEx site that I use quite often.
Regular Expression Library
You should be able to find what you're looking for there.

Related

Find and replace a specific number with regex

I have the following string
string absoluteUri = "http://localhost/asdf1234?$asdf=1234&$skip=1234&skip=4321&$orderby=asdf"
In this string I would like to replace '$skip=1234' with '$skip=1244'
I have tried the following regular expression:
Regex.Replace(absoluteUri, #"$skip=\d+", "$skip=1244");
Unfortunately this is not working. What am I doing wrong?
The output should be:
"http://localhost/asdf1234?$asdf=1234&$skip=1244&skip=4321&$orderby=asdf"
$ is a special character in regular expressions (it's an anchor). You need to escape it in both the expression and in the replacement string, but they are escaped differently.
In the regular expression, you escape it with a \ but in the substitution you escape it by adding another $:
Regex.Replace(absoluteUri, #"\$skip=\d+", "$$skip=1244");
I can't add comment.
Just little fix. Need to do:
absoluteUri = Regex.Replace(absoluteUri, #"\$skip=\d+", "$skip=1244");

Regular expression for matching start of string with

I need help to built regular expression for
string which does not start with pcm_ or PCM_
any guess!!!
No need to use regular expression. Use String.startsWith() method.
if (!str.StartsWith("pcm_",StringComparison.InvariantCultureIgnoreCase)) {}
if (String.startsWith("pcm_") || String.startsWith("PCM_"))
{
//...
}
The regex solution would be
^(?i)(?!pcm_)
(?i) is the inline version of RegexOptions.IgnoreCase
^ matches the start of the string
(?!pcm_) is a negative lookahead assertion, that is true if the string does not start with "pcm_" or "PCM_" (but also "PcM_, ...)
As already pointed out, you don't need to use regular expressions for this, but if you wanted to you could use one with negative lookahead like so: ^(?!pcm_|PCM_).*$
see similar link
Regex pattern for checking if a string starts with a certain substring?
No need for a Regex here, simply use String.StartsWith http://msdn.microsoft.com/en-us/library/system.string.startswith.aspx

C# regex not matching string

I have a string which is formatted like this: $20,$40,$AA,$FF. Basically, hex numbers and they can be of many bytes. I want to check if a string is in the above format, so I tried something like this:
string a = "$20,$30,$40";
Regex reg = new Regex(#"$[0-9a-fA-F],");
if (a.StartsWith(string.Format("{0}{1}", reg, reg)))
MessageBox.Show("A");
It doesn't seem to work though, is there anything I'm missing?
$ is a special character in regular expressions and means end of string. That regex won't match anything at all since you're specifying stuff after the string end. Escape the $ character like
"\$[0-9a-fA-F]{2},"
Anyway AFAIK this will not work with your string since it doesn't end with an ",". You might try:
"^(\$[0-9a-fA-F]{2},?)+$"
You can even simplify the regex by using case-insensitive regex matching:
Regex reg = new Regex(#"^(\$[0-9A-F]{2},?)+$", RegexOptions.IgnoreCase);
EDIT: corrected to match exactly 2 hexadecimal digits.
EDIT: maybe you should write your regex checking like:
if (Regex.IsMatch(a,#"^(\$[0-9A-F]{2},?)+$",RegexOptions.IgnoreCase))
{
// Do whatever
}
I think you are missing a quantifier:
"\$[0-9a-fA-F]+,"
For the problem with the comma at the end, I would simply append one at the end to keep the regex as simple as possible. But this is just the way I would do it.
There are 3 things that need to be changed:
Need to escape your $ symbol as it represents end of line.
\$
Need to tweak your regex pattern to match the entire string instead of parts.
^(\$[0-9a-fA-F]{2},+)+\$[0-9a-fA-F]{2}$
Need to change your code to use Regex.IsMatch.
string a = "$20,$30,$40";
if (Regex.IsMatch(a,#"^(\$[0-9a-fA-F]{2},+)+\$[0-9a-fA-F]{2}$",RegexOptions.IgnoreCase))
MessageBox.Show("A");
PS:
If the input string has white space like a tab or a space in between, then this regex will need to be modified. In such cases, you have to use "\s" at the right positions. For example, if you have white space around the commas like
string a = "$20 ,$30, $40";
then you need to tweak your RegEx this way:
^(\$[0-9a-fA-F]{2}\s*,+\s*)+\$[0-9a-fA-F]{2}\s*$
References:
C# Regex Testers
A Better .NET Regular Expression Tester
RegexHero tester
about Regex.IsMatch (instead of using Match)
MSDN Regex.isMatch
Usage example
C# Regular Expression Cheat Sheet
Old answer below (Ignore):
Try this:
"\$[0-9a-fA-F]{2}?[,]{0,1}"
You might also want to add a repeat modifier to your set such that it becomes;
"\$[0-9a-fA-F]+,"

How do I replace an actual asterisk character (*) in a Regex expression?

I have a statement:
I have a string such as
content = "* test *"
I want to search and replace it with so when I am done the string contains this:
content = "(*) test (*)"
My code is:
content = Regex.Replace(content, "*", "(*)");
But this causes an error in C# because it thinks that the * is part of the Regular Expressions Syntax.
How can I modify this code so it changes all asterisks in my string to (*) instead without causing a runtime error?
Since * is a regex metacharacter, when you need it as a literal asterisk outside of a character class definition, it needs to be escaped with \ to \*.
In C#, you can write this as "\\*" or #"\*".
C# should also have a general purpose "quoting" method so that you can quote an arbitrary string and match it as a literal.
See also
Regular expressions and escaping special characters
Full list of what needs to be escaped, where/when
You can escape it:
\*
You don't need a regular expression in this simple scenario. You can use String.Replace:
content = content.Replace("*", "(*)");
Use Regex.Escape() It will take all of the string and make it into something you can use as part of a regex.
Use \\* instead of * in regex.replace call

Filtering out bad characters using a regular expression

I want to filter out the char ^ before searching something in a database.
What will my regular expression look like if I want to achieve that the query will ignore the sign ^?
I'm working with Visual Studio 2008, .NET 3.5, and C#.
You don't need a regex for that. You can simply do this:
myString = myString.Replace("^", "");
If you want to filter only that character, a simple String.Replace() call would suffice.
Anyway, if you want to use a regular expression, you must escape the ^, since it is a special character.
myString = Regex.Replace(myString, "\^+", String.Empty);

Categories