How to remove string "[xyz]" from filename with Regex? - c#

I have the filename 0154562A5BS16101[001] in which I would like to remove the "[001]" to just leave 0154562A5BS16101.
I have tried using the regex:
var output = Regex.Replace(filename, #"[]", string.Empty);
But it throws:
System.ArgumentException: 'parsing '[]' - Unterminated [] set.'
I feel like this is a pretty easy for Regex masters, but I don't have much experience with Regex.

Since [ and ] are metacharacters in regex language, you need to escape them. You also need to tell regex that you want to match everything up to the closing square bracket:
var output = Regex.Replace(filename, #"\[[^\]]*\]", string.Empty);
\[ and \] at the ends are square brackets that you want to replace. The [^\]]* section in the middle matches any number of characters other than the closing square bracket.
Demo.

Related

Remove some specific string with special character

Input String
string b = "14-03-002980 AND 14-03- [ ] (5)Description of 002981";
In output String I Want Result As
4-03-002980 AND 14-03-002981
I tried with below regex but it, not works
Regex.Replace(b, "[#&'(\\s)<>(5)Description of ]","");
Plaese, help me if anyone knows how to do this thing.
You can use this regex,
\s+\[.*(?=\b\d+)
and replace it with empty string.
You start with one or more whitespace then match a [ using \[ and then .* consumes all the characters greedily and only stops when it sees a number using positive look ahead (?=\b\d+)
Regex Demo

Remove whitespace before or after a character with regex

I new in regex and i want to find a good solution for replacing whitespace before or after the / char in my sub string.
I have got string like
"Path01 /Some folder/ folder (2)"
i checked regex
#"\s?()\s?"
but this incorrect for me. I must get in output
Path01/Some folder/folder (2)
Can you help me?
Thanks!
You may use
#"\s*/\s*"
and replace with /.
See the regex demo
The pattern matches zero or more (*) whitespace chars (\s), then a / and then again 0+ whitespace chars.
C#:
var result = Regex.Replace(s, #"\s*/\s*", "/");

c# Regex.Replace [^\w ] that also removes underscores?

So I have spent far too long on this and have tried tons of things with no luck. I think I am just bad at regex. I am trying to clean a string of ALL non alpha numeric characters but leaving spaces. I DO NOT WANT TO USE [^A-Za-z0-9 ]+ due language concerns.
Here are a few things I have tried:
cleaned_string = Regex.Replace(input_string, #"[^\w ]+[_]+);
cleaned_string = Regex.Replace(input_string, ([^\w ]+)([_]+));
cleaned_string = Regex.Replace(input_string, [^ \w?<!_]+);
Edit: Solved thanks to a very helpful person below.
My final product ended up being this: [_]+|[^\w\s]+
Thanks for all the help!
This should work for you
// Expression: _|[^\w\d ]
cleaned_string = Regex.Replace(input_string, #"/_|[^\w\d ]", "");
You may use
var res = Regex.Replace(s, #"[\W_-[\s]]+", string.Empty);
See the regex demo.
Look at \W pattern: it matches any non-word chars. Now, you want to exclude a whitespace matching pattern from \W - use character class subtraction: [\W-[\s]]. This matches any char \W matches except what \s matches. And to also match a _, just add it to the character class. Add + quantifier to remove whole consecutive chunks of matching chars at one go.
Details
[ - start of a character class
\W_ - any non-word or _ chars
-[\s] - except for chars matched with \s (whitespace) pattern
] - end of the character class
+ - one or more times.

Using an escape character with a beginning wildcard in regex in c#

Below is a sample of an email I am using from a database:
2.2|[johnnyappleseed#example.com]
Every line is different, and it may or may not be an email, but it will always. I am trying to use regular expressions to get the information inside the brackets. Below is what I have been trying to use:
^\[\]$
Unfortunately, every time I try to use it, the expression isn't matching. I think the problem is using the escape characters, but I am not sure. If this is not how I use the escape characters with this, or if I am wrong completely, please let me know what the actual regex should be.
Close to yours is ^.*\[(.*)\]$:
^ start of the line
.* anything
\[ a bracket, indicating the start of the email
(.*) anything (the email), as a capturing group
\] a square bracked, indicating the end of the email
$ end of the line
Note that your Regex is missing the .* parts to match the things between the key characters [ and ].
Your regex - ^\[\]$ - matches a single string/line that only contains [], and you need to obtain a substring inbetween the square brackets somewhere further inside a larger string.
You can use
var rx = new Regex(#"(?<=\[)[^]]+");
Console.WriteLine(rx.Match(s).Value);
See regex demo
With (?<=\[) we find the position after [ and then we match every character that is not ] with [^]]+.
Another, non-regex way:
var s = "2.2|[johnnyappleseed#example.com]";
var ss = s.Split('|');
if (ss.GetLength(0) > 1)
{
var last = ss[ss.GetLength(0)-1];
if (last.Contains("[") && last.Contains("#")) // We assume there is an email
Console.WriteLine(last.Trim(new[] {'[', ']'}));
}
See IDEONE demo of both approaches

Star - look for the character * in a string using regex

I am trying to find the following text in my string : '***'
the thing is that the C# Regex mechanism doesnt allow me to do the following:
new Regex("***", RegexOptions.CultureInvariant | RegexOptions.Compiled);
due to
ArgumentException: "parsing "*" - Quantifier {x,y} following nothing."
obviously it thinks that my stars represents regular expressions,
is there a way to tell the Regex mechanism to treat stars as just stars and nothing else?
* in Regex means:
Matches the previous element zero or more times.
so that, you need to use \* or [*] instead.
explain:
\
When followed by a character that is not recognized as an escaped character in this and other tables in this topic, matches that character. For example, \* is the same as \x2A.
[ character_group ]
Matches any single character in character_group.
You need to escape the star with a backslash: #"\*"

Categories