C# Regular Expression For Specific Characters [duplicate] - c#

This question already has answers here:
regex 'literal' meaning in regex
(1 answer)
How to make a regex match case insensitive?
(1 answer)
Closed 4 years ago.
I need to build regex dynamically, so I pass to my method a string of valid characters. Then I use that string to build regex in my method
string valid = "^m><"; // Note 1st char is ^ (special char)
string input = ...; //some string I want to check
Check(valid);
public void Check(string valid)
{
Regex reg = new Regex("[^" + valid + "]");
if (reg.Match(input).ToString().Length > 0)
{
throw new Exception(...);
}
}
I want above Match to throw exception if it finds any other character than characters provided by valid string above. But in my case, even if I dont have any other character tahn these 3, Check method still throws new exception.
What is wrong with this regex?

this resolved it, thanks to everyone for help
Regex reg = new Regex("[^" + valid + "]", RegexOptions.IgnoreCase);

Related

String.Remove throws ArgumentOutOfRangeException [duplicate]

This question already has answers here:
Remove characters from C# string
(22 answers)
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 1 year ago.
My code is parsing a text file, looking for ID numbers that match the pattern "####-####-#". Once I find a number that matches this pattern, I need to strip out the dashes and store it in a string variable. I'm trying to use String.Remove to remove this character, but keep getting the OutOfRangeException for some reason.
Here's my code:
//regex for the ID number pattern
Regex pattern = new Regex("^[0-9]{4}-[0-9]{4}-[0-9]{1}$");
//StreamReader to iterate thru the file line-by-line
using (StreamReader reader = new StreamReader(pathToMyFile))
{
while (!reader.EndOfStream)
{
readLine = reader.ReadLine();
//the number I want is always at the beginning of the line, so I capture the
//first 11 characters for regex comparison
string possibleMatch = readLine.Substring(0, 11);
if (!String.IsNullOrEmpty(possibleMatch) &&
pattern.Match(possibleMatch).Success)
{
//If possibleMatch isn't blank, and matches the regex, we found an ID
string match = possibleMatch.Remove('-');
}
}
}
When I try to remove the dashes, I get this error:
System.ArgumentOutOfRangeException: 'startIndex must be less than length of string.
Parameter name: startIndex'
The error is always thrown on the possibleMatch.Remove('-') method, never on readLine.Substring(0, 11). Any advice is appreciated.

Find String and replace with left String with Regular Expression in C# [duplicate]

This question already has answers here:
How do I read and parse an XML file in C#?
(12 answers)
Closed 2 years ago.
I have an xml file and I wanted to search the file using the regular expression.
My used regular expression:
(? <= <Name> Description <\ / Name> <Value>). *? (? = <\ / Value>)
And replace the expressions found by a left(expression, 15). There are situations where the string is too long and you need to truncate to left 15.
Example:
https://regex101.com/r/Etfpol/3
The text found is:
Solution changed from
Resolved Time changed
Updated By changed from
I want to replace:
Solution change
Resolved Time c
Updated By chan
My tried Code:
System.IO.StreamReader file = new System.IO.StreamReader(Dts.Connections["xmlfile.xml"].ConnectionString);
Dts.Variables["varXML"].Value = file.ReadLine();
String teste = Dts.Variables["varXML"].Value.ToString();
string pattern = #"(?<=<Name>Description<\/Name><Value>).*?(?=<\/Value>)";
string result = Regex.Replace(teste, pattern, ); Dts.Variables["varXML"].Value = result;
Thanks.
first, for reference, your original Regex from your example was
(?<=<Name>Description<\/Name><Value>).*?(?=<\/Value>)
I modified it to this:
(?<=<Name>Description<\/Name><Value>)(?<Text>.{0,15}).*?(?=<\/Value>)
It captures now the first up to 15 characters in the named group 'Text' and discards the remaining characters, if there are any.
Now you can simply output just the named group:
${Text}
Here is your modified example: https://regex101.com/r/Etfpol/4
Try this to get a replacement result:
string result = Regex.Replace(teste, pattern, delegate(Match match)
{
string v = match.ToString();
return v.Substring(0, 15);
});
Source: system.text.regularexpressions.regex.replace
I use matchevaluator Delegate for this.

Regex not able to parse last group [duplicate]

This question already has answers here:
What is the difference between .*? and .* regular expressions?
(3 answers)
Closed 3 years ago.
This is the test. I expect the last group to be ".png", but this pattern returns "" instead.
var inputStr = #"C:\path\to\dir\[yyyy-MM-dd_HH-mm].png";
var pattern = #"(.*?)\[(.*?)\](.*?)";
var regex = new Regex(pattern);
var match = regex.Match(inputStr);
var thirdGroupValue = match.Groups[3].Value;
// ✓ EXPECTED: ".png"
// ✗ CURRENT: ""
The 1st and 2nd groups work fine.
This is because you made the * in Group 3 lazy:
(.*?)\[(.*?)\](.*?)
^
here
This means it will match as little as possible. What's the least .* can match? An empty string!
You can learn more about lazy vs greedy here.
You can fix this either by removing ?, making it greedy, or put a $ at the end, telling it to match until the end of the string:
(.*?)\[(.*?)\](.*)
or
(.*?)\[(.*?)\](.*?)$

Find a pattern into a string without space [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I'm looking for a way to find and extract the string matching a pattern in a string without a space :
string regexpattern = #"[A-Z]{4}\d{4}$"; // ex : BERF4787
string stringWithoutSpace = "stringsampleBERF4787withpattern";
string stringMatchPattern = ??? //I want to get BEFR4787 in this variable
You are almost there. The problem in your pattern is the $ which matches the end of a string. Since in your example the "BERF4787" is located in the middle of the string you should simply remove it:
string regexpattern = #"[A-Z]{4}\d{4}"; // ex : BERF4787
string stringWithoutSpace = "stringsampleBERF4787withpattern";
If you want to match your pattern in a string you can use the Regex.Match method which returns an object of type Match.
To get the matched value you need to use the Match.Value property like this:
string stringMatchPattern = Regex.Match(stringWithoutSpace, regexpattern).Value;

Regex only checks first character in string C# [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
Why does the following method only check the first character in the supplied string?
public static bool IsUnicodeSms(string message)
{
var strMap = new Regex(#"^[#£$¥èéùìòÇØøÅå_ÆæßÉ!""#%&'()*+,-./0123456789:;<=>? ¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^{}\[~]|€]+$");
return !strMap.IsMatch(message);
}
So for example the following string returns false: "abcლ" but "ლabc" returns true.
You have to escape ] with \] and also put the - at the end:
Change this:
var strMap = new Regex(#"^[#£$¥èéùìòÇØøÅå_ÆæßÉ!""#%&'()*+,-./0123456789:;<=>? ¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^{}\[~]|€]+$");
To this:
var strMap = new Regex(#"^[#£$¥èéùìòÇØøÅå_ÆæßÉ!""#%&'()*+,./0123456789:;<=>? ¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^{}\[~\]|€-]+$");
Btw, you can improve your regex and use:
var strMap = new Regex(#"^[#£$¥èéùìòÇØøÅå_ÆæßÉ!"#%&'()*+,./\w:;<=>? ¡ÄÖÑܧ¿äöñüà^{}\[~\]|€-]+$");
And not sure if using the ignore case flag might help you to shorten it a little more like this:
var strMap = new Regex(#"(?i)^[#£$¥èéùìòÇøå_Ææß!"#%&'()*+,./\w:;<=>? ¡§¿äöñüà^{}\[~\]|€-]+$");
You copied the code from here.
It's very flawed. It needs more escaping. From Regexp Tutorial - Character Classes or Character Sets:
the only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (\), the caret (^), and the hyphen (-)
So, it needs to be:
new Regex(#"^[#£$¥èéùìòÇØøÅå_ÆæßÉ!""#%&'()*+,\-./0123456789:;<=>? ¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^{}\[~\]|€]+$");
You can of course improve the regex even further like #Fede demonstrates.

Categories