Unusual Regex behavior in c# - c#

I have a Regex that is behaving rather oddly and I can't figure why. Original Regex:
Regex regex = new Regex(#"(?i)\d\.\d\dv");
This expression returns/matches an equivalent to 1.35V or 1.35v, which is what I want. However, it is not exclusive enough for my program and it returns some strings I don't need.
Modified Regex:
Regex rgx = new Regex(#"(?i)\d\.\d\dv\s");
Simply by adding '\s' to the expression, it matches/returns DDR3, which is not at all what I want. I'm guessing some sort of inversion is occurring, but I don't understand why and I can't seem to find a reference to explain it. All I wanted to do was add a space to the end of expression to filter a few more results.
Any help would be greatly appreciated.
EDIT:
Here is a functional test case with a generic version of what is going on in my code. Just open a new WPF in Visual Studio, copy and paste, and it should repeat the results for you.
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Regex rgx1 = new Regex(#"(?i)\d\.\d\dv");
Regex rgx2 = new Regex(#"(?i)\d\.\d\dv\s");
string testCase = #"DDR3 Vdd | | | | | 1.35v |";
string str = null;
public void IsMatch(string input)
{
Match rgx1Match = rgx1.Match(input);
if (rgx1Match.Success)
{
GetInfo(input);
}
}
public void GetInfo(string input)
{
Match rgx1Match = rgx1.Match(input);
Match rgx2Match = rgx2.Match(input);
string[] tempArray = input.Split();
int index = 0;
if (rgx1Match.Success)
{
index = GetMatchIndex(rgx1, tempArray);
str = tempArray[index].Trim();
global::System.Windows.Forms.MessageBox.Show("First expression match: " + str);
}
if (rgx2Match.Success)
{
index = GetMatchIndex(rgx2, tempArray);
str = tempArray[index].Trim();
System.Windows.Forms.MessageBox.Show(input);
global::System.Windows.Forms.MessageBox.Show("Second expression match: " + str);
}
}
public int GetMatchIndex(Regex expression, string[] input)
{
int index = 0;
for (int i = 0; i < input.Length; i++)
{
if (index < 1)
{
Match rgxMatch = expression.Match(input[i]);
if (rgxMatch.Success)
{
index = i;
}
}
}
return index;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
string line;
IsMatch(testCase);
}
}
}
The GetMatchesIndex method is called a number of times in other parts of the code without incident, it is just on this one Regex that I've hit a stumbling block.

The behavior you are seeing has entirely to do with your application logic, and very little to do with the regular expression. In GetMatchIndex, you are defaulting index = 0. So what happens if none of the entries in string[] input match? You get back index = 0, which is the index of DDR3, the first element in string[] input.
You don't see that behavior in the first regular expression, because it matches 1.35v. However, when you add the space to the end, it doesn't match any of the entries in the split input, so you get back the first one by default which happens to be DDR3. Also, if (rgx1Match.Success) doesn't really help, because you check for a match in the entire string first (which does match because there's a space there), and then search for the index after splitting, which removed the spaces!
The fix is pretty simple: When you are returning an index from an array in a programming language that uses 0-based numbering, the standard way to represent "not found" is with -1 so it doesn't get confused with the valid result of 0. So default index to -1 instead and handle a result of -1 as a special case, i.e., display an error message to the user like "No matches".

Your question is incorrect:
new Regex(#"(?i)\d\.\d\dv\s").Match("DDR3").Success
is false
In fact, the results seem to work exactly as you'd like.

Related

How to split a string in razor with no delimeters only size and position [duplicate]

The .NET Framework gives us the Format method:
string s = string.Format("This {0} very {1}.", "is", "funny");
// s is now: "This is very funny."
I would like an "Unformat" function, something like:
object[] params = string.Unformat("This {0} very {1}.", "This is very funny.");
// params is now: ["is", "funny"]
I know something similar exists in the ANSI-C library (printf vs scanf).
The question: is there something similiar in C#?
Update: Capturing groups with regular expressions are not the solution I need. They are also one way. I'm looking for a system that can work both ways in a single format. It's OK to give up some functionality (like types and formatting info).
There's no such method, probably because of problems resolving ambiguities:
string.Unformat("This {0} very {1}.", "This is very very funny.")
// are the parameters equal to "is" and "very funny", or "is very" and "funny"?
Regular expression capturing groups are made for this problem; you may want to look into them.
Regex with grouping?
/This (.*?) very (.*?)./
If anyone's interested, I've just posted a scanf() replacement for .NET. If regular expressions don't quite cut it for you, my code follows the scanf() format string quite closely.
You can see and download the code I wrote at http://www.blackbeltcoder.com/Articles/strings/a-sscanf-replacement-for-net.
You could do string[] parts = string.Split(' '), and then extract by the index position parts[1] and parts [3] in your example.
Yep. These are called "regular expressions". The one that will do the thing is
This (?<M0>.+) very (?<M1>.+)\.
#mquander: Actualy, PHP solves it even different:
$s = "This is very very funny.";
$fmt = "This %s very %s.";
sscanf($s, $fmt, $one, $two);
echo "<div>one: [$one], two: [$two]</div>\n";
//echo's: "one: [is], two: [very]"
But maybe your regular expression remark can help me. I just need to rewrite "This {0} very {1}." to something like: new Regex(#"^This (.*) very (.*)\.$"). This should be done programmatical, so I can use one format string on the public class interface.
BTW: I've already have a parser to find the parameters: see the Named Format Redux blog entry by Phil Haack (and yes, I also want named paramters to work both ways).
I came across the same problem, i belive that there is a elegante solution using REGEX... but a came up with function in C# to "UnFormat" that works quite well. Sorry about the lack of comments.
/// <summary>
/// Unformats a string using the original formating string.
///
/// Tested Situations:
/// UnFormat("<nobr alt=\"1\">1<nobr>", "<nobr alt=\"{0}\">{0}<nobr>") : "1"
/// UnFormat("<b>2</b>", "<b>{0}</b>") : "2"
/// UnFormat("3<br/>", "{0}<br/>") : "3"
/// UnFormat("<br/>4", "<br/>{0}") : "4"
/// UnFormat("5", "") : "5"
/// UnFormat("<nobr>6<nobr>", "<nobr>{0}<nobr>") : "6"
/// UnFormat("<nobr>2009-10-02<nobr>", "<nobr>{0:yyyy-MM-dd}<nobr>") : "2009-10-02"
/// UnFormat("<nobr><nobr>", "<nobr>{0}<nobr>") : ""
/// UnFormat("bla", "<nobr>{0}<nobr>") : "bla"
/// </summary>
/// <param name="original"></param>
/// <param name="formatString"></param>
/// <returns>If an "unformat" is not possible the original string is returned.</returns>
private Dictionary<int,string> UnFormat(string original, string formatString)
{
Dictionary<int, string> returnList = new Dictionary<int, string>();
try{
int index = -1;
// Decomposes Format String
List<string> formatDecomposed = new List<string> (formatString.Split('{'));
for(int i = formatDecomposed.Count - 1; i >= 0; i--)
{
index = formatDecomposed[i].IndexOf('}') + 1;
if (index > 0 && (formatDecomposed[i].Length - index) > 0)
{
formatDecomposed.Insert(i + 1, formatDecomposed[i].Substring(index, formatDecomposed[i].Length - index));
formatDecomposed[i] = formatDecomposed[i].Substring(0, index);
}
else
//Finished
break;
}
// Finds and indexes format parameters
index = 0;
for (int i = 0; i < formatDecomposed.Count; i++)
{
if (formatDecomposed[i].IndexOf('}') < 0)
{
index += formatDecomposed[i].Length;
}
else
{
// Parameter Index
int parameterIndex;
if (formatDecomposed[i].IndexOf(':')< 0)
parameterIndex = Convert.ToInt16(formatDecomposed[i].Substring(0, formatDecomposed[i].IndexOf('}')));
else
parameterIndex = Convert.ToInt16(formatDecomposed[i].Substring(0, formatDecomposed[i].IndexOf(':')));
// Parameter Value
if (returnList.ContainsKey(parameterIndex) == false)
{
string parameterValue;
if (formatDecomposed.Count > i + 1)
if (original.Length > index)
parameterValue = original.Substring(index, original.IndexOf(formatDecomposed[i + 1], index) - index);
else
// Original String not valid
break;
else
parameterValue = original.Substring(index, original.Length - index);
returnList.Add(parameterIndex, parameterValue);
index += parameterValue.Length;
}
else
index += returnList[parameterIndex].Length;
}
}
// Fail Safe #1
if (returnList.Count == 0) returnList.Add(0, original);
}
catch
{
// Fail Safe #2
returnList = new Dictionary<int, string>();
returnList.Add(0, original);
}
return returnList;
}
I reference earlier reply, wrote a sample see following
string sampleinput = "FirstWord.22222";
Match match = Regex.Match(sampleinput, #"(\w+)\.(\d+)$", RegexOptions.IgnoreCase);
if(match.Success){
string totalmatchstring = match.Groups[0]; // FirstWord.22222
string firstpart = match.Groups[1]; // FirstWord`
string secondpart = match.Groups[2]; // 22222
}

How to check how many times a string exist in another string

I Want to see how many time's a string occurrs in a string. For example I want to see how many times 2018 occurs in this paragraph:
zaeazeaze2018
azeazeazeazeaze2018azezaaze
azeaze4azeaze2018
In this case it is occuring 3 times.
I tried the following code
But the problem is that it always returns 0
And I can't find the mistake here:
public static string count(string k)
{
int i = 0;
foreach(var line in k)
{
if (line.ToString().Contains("Bestellung sehen"))
{
i++;
i = +i;
}
}
return i.ToString();
}
use this :
string text = "Hello2018,world2018\r\nWe have five 2018 here\r\n2018is coming2018"
int Counter = Regex.Matches(text, "2018").Count;
Console.WriteLine(Counter.ToString()); //write : 5
You can use Regular Expressions to handle such cases. Regular expressions give you good flexibility over your pattern matching in a string. In your case, I have prepared a sample code for you using Regular Expressions:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string str="zaeazeaze2018azeazeazeazeaze2018azezaazeazeaze4azeaze2018";
string regexPattern = #"2018";
int numberOfOccurence = Regex.Matches(str, regexPattern).Count;
Console.WriteLine(numberOfOccurence);
}
}
Working example: https://dotnetfiddle.net/PGgbm8
If you will notice the line string regexPattern = #"2018";, this sets the pattern to find all occurences of 2018 from your string. You can change this pattern according to what you require. A simple example would be that if I changed the pattern to string regexPattern = #"\d+";, it would give me 4 as output. This is because my pattern will match all occurences of numbers in the string.
This can be accomplished using Regular Expressions with the following:
using System.Text.RegularExpressions;
public static int count(string fullString, string searchPattern)
{
int i = Regex.Matches(fullString, searchPattern).Count;
return i;
}
For example, the following returns 2 as an int, not string:
count("asdfasdfasfdfindmeasdfadfasdasdfasdffindmesadf","findme")
I find this is quick enough for most of my use cases.
str is your String from your count method
str2 is your substring which is Bestellung sehen
int n = str2.length
int k = 0;
for(int i=0;i < str.length; i++){
if(str.substring(i,i+n-1)){
k++;
if(i+n-1 >= str.length){
break;
}
}
}
return k.toString()

How to split at every second quotation mark

I have a string that looks like this
2,"E2002084700801601390870F"
3,"E2002084700801601390870F"
1,"E2002084700801601390870F"
4,"E2002084700801601390870F"
3,"E2002084700801601390870F"
This is one whole string, you can imagine it being on one row.
And I want to split this in the way they stand right now like this
2,"E2002084700801601390870F"
I cannot change the way it is formatted. So my best bet is to split at every second quotation mark. But I haven't found any good ways to do this. I've tried this https://stackoverflow.com/a/17892392/2914876 But I only get an error about invalid arguements.
Another issue is that this project is running .NET 2.0 so most LINQ functions aren't available.
Thank you.
Try this
var regEx = new Regex(#"\d+\,"".*?""");
var lines = regex.Matches(txt).OfType<Match>().Select(m => m.Value).ToArray();
Use foreach instead of LINQ Select on .Net 2
Regex regEx = new Regex(#"\d+\,"".*?""");
foreach(Match m in regex.Matches(txt))
{
var curLine = m.Value;
}
I see three possibilities, none of them are particularly exciting.
As #dvnrrs suggests, if there's no comma where you have line-breaks, you should be in great shape. Replace ," with something novel. Replace the remaining "s with what you need. Replace the "something novel" with ," to restore them. This is probably the most solid--it solves the problem without much room for bugs.
Iterate through the string looking for the index of the next " from the previous index, and maintain a state machine to decide whether to manipulate it or not.
Split the string on "s and rejoin them in whatever way works the best for your application.
I realize regular expressions will handle this but here's a pure 2.0 way to handle as well. It's much more readable and maintainable in my humble opinion.
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
const string data = #"2,""E2002084700801601390870F""3,""E2002084700801601390870F""1,""E2002084700801601390870F""4,""E2002084700801601390870F""3,""E2002084700801601390870F""";
var parsedData = ParseData(data);
foreach (var parsedDatum in parsedData)
{
Console.WriteLine(parsedDatum);
}
Console.ReadLine();
}
private static IEnumerable<string> ParseData(string data)
{
var results = new List<string>();
var split = data.Split(new [] {'"'}, StringSplitOptions.RemoveEmptyEntries);
if (split.Length % 2 != 0)
{
throw new Exception("Data Formatting Error");
}
for (var index = 0; index < split.Length / 2; index += 2)
{
results.Add(string.Format(#"""{0}""{1}""", split[index], split[index + 1]));
}
return results;
}
}
}

Check string for invalid characters? Smartest way?

I would like to check some string for invalid characters. With invalid characters I mean characters that should not be there. What characters are these? This is different, but I think thats not that importan, important is how should I do that and what is the easiest and best way (performance) to do that?
Let say I just want strings that contains 'A-Z', 'empty', '.', '$', '0-9'
So if i have a string like "HELLO STaCKOVERFLOW" => invalid, because of the 'a'.
Ok now how to do that? I could make a List<char> and put every char in it that is not allowed and check the string with this list. Maybe not a good idea, because there a lot of chars then. But I could make a list that contains all of the allowed chars right? And then? For every char in the string I have to compare the List<char>? Any smart code for this? And another question: if I would add A-Z to the List<char> I have to add 25 chars manually, but these chars are as I know 65-90 in the ASCII Table, can I add them easier? Any suggestions? Thank you
You can use a regular expression for this:
Regex r = new Regex("[^A-Z0-9.$ ]$");
if (r.IsMatch(SomeString)) {
// validation failed
}
To create a list of characters from A-Z or 0-9 you would use a simple loop:
for (char c = 'A'; c <= 'Z'; c++) {
// c or c.ToString() depending on what you need
}
But you don't need that with the Regex - pretty much every regex engine understands the range syntax (A-Z).
I have only just written such a function, and an extended version to restrict the first and last characters when needed. The original function merely checks whether or not the string consists of valid characters only, the extended function adds two integers for the numbers of valid characters at the beginning of the list to be skipped when checking the first and last characters, in practice it simply calls the original function 3 times, in the example below it ensures that the string begins with a letter and doesn't end with an underscore.
StrChr(String, "_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
StrChrEx(String, "_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", 11, 1));
BOOL __cdecl StrChr(CHAR* str, CHAR* chars)
{
for (int s = 0; str[s] != 0; s++)
{
int c = 0;
while (true)
{
if (chars[c] == 0)
{
return false;
}
else if (str[s] == chars[c])
{
break;
}
else
{
c++;
}
}
}
return true;
}
BOOL __cdecl StrChrEx(CHAR* str, CHAR* chars, UINT excl_first, UINT excl_last)
{
char first[2] = {str[0], 0};
char last[2] = {str[strlen(str) - 1], 0};
if (!StrChr(str, chars))
{
return false;
}
if (excl_first != 0)
{
if (!StrChr(first, chars + excl_first))
{
return false;
}
}
if (excl_last != 0)
{
if (!StrChr(last, chars + excl_last))
{
return false;
}
}
return true;
}
If you are using c#, you do this easily using List and contains. You can do this with single characters (in a string) or a multicharacter string just the same
var pn = "The String To ChecK";
var badStrings = new List<string>()
{
" ","\t","\n","\r"
};
foreach(var badString in badStrings)
{
if(pn.Contains(badString))
{
//Do something
}
}
If you're not super good with regular expressions, then there is another way to go about this in C#. Here is a block of code I wrote to test a string variable named notifName:
var alphabet = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
var numbers = "0,1,2,3,4,5,6,7,8,9";
var specialChars = " ,(,),_,[,],!,*,-,.,+,-";
var validChars = (alphabet + "," + alphabet.ToUpper() + "," + numbers + "," + specialChars).Split(',');
for (int i = 0; i < notifName.Length; i++)
{
if (Array.IndexOf(validChars, notifName[i].ToString()) < 0) {
errorFound = $"Invalid character '{notifName[i]}' found in notification name.";
break;
}
}
You can change the characters added to the array as needed. The Array IndexOf method is the key to the whole thing. Of course if you want commas to be valid, then you would need to choose a different split character.
Not enough reps to comment directly, but I recommend the Regex approach. One small caveat: you probably need to anchor both ends of the input string, and you will want at least one character to match. So (with thanks to ThiefMaster), here's my regex to validate user input for a simple arithmetical calculator (plus, minus, multiply, divide):
Regex r = new Regex(#"^[0-9\.\-\+\*\/ ]+$");
I'd go with a regex, but still need to add my 2 cents here, because all the proposed non-regex solutions are O(MN) in the worst case (string is valid) which I find repulsive for religious reasons.
Even more so when LINQ offers a simpler and more efficient solution than nesting loops:
var isInvalid = "The String To Test".Intersect("ALL_INVALID_CHARS").Any();

Count regex replaces (C#)

Is there a way to count the number of replacements a Regex.Replace call makes?
E.g. for Regex.Replace("aaa", "a", "b"); I want to get the number 3 out (result is "bbb"); for Regex.Replace("aaa", "(?<test>aa?)", "${test}b"); I want to get the number 2 out (result is "aabab").
Ways I can think to do this:
Use a MatchEvaluator that increments a captured variable, doing the replacement manually
Get a MatchCollection and iterate it, doing the replacement manually and keeping a count
Search first and get a MatchCollection, get the count from that, then do a separate replace
Methods 1 and 2 require manual parsing of $ replacements, method 3 requires regex matching the string twice. Is there a better way.
Thanks to both Chevex and Guffa. I started looking for a better way to get the results and found that there is a Result method on the Match class that does the substitution. That's the missing piece of the jigsaw. Example code below:
using System.Text.RegularExpressions;
namespace regexrep
{
class Program
{
static int Main(string[] args)
{
string fileText = System.IO.File.ReadAllText(args[0]);
int matchCount = 0;
string newText = Regex.Replace(fileText, args[1],
(match) =>
{
matchCount++;
return match.Result(args[2]);
});
System.IO.File.WriteAllText(args[0], newText);
return matchCount;
}
}
}
With a file test.txt containing aaa, the command line regexrep test.txt "(?<test>aa?)" ${test}b will set %errorlevel% to 2 and change the text to aabab.
You can use a MatchEvaluator that runs for each replacement, that way you can count how many times it occurs:
int cnt = 0;
string result = Regex.Replace("aaa", "a", m => {
cnt++;
return "b";
});
The second case is trickier as you have to produce the same result as the replacement pattern would:
int cnt = 0;
string result = Regex.Replace("aaa", "(?<test>aa?)", m => {
cnt++;
return m.Groups["test"] + "b";
});
This should do it.
int count = 0;
string text = Regex.Replace(text,
#"(((http|ftp|https):\/\/|www\.)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])?)", //Example expression. This one captures URLs.
match =>
{
string replacementValue = String.Format("<a href='{0}'>{0}</a>", match.Value);
count++;
return replacementValue;
});
I am not on my dev computer so I can't do it right now, but I am going to experiment later and see if there is a way to do this with lambda expressions instead of declaring the method IncrementCount() just to increment an int.
EDIT modified to use a lambda expression instead of declaring another method.
EDIT2 If you don't know the pattern in advance, you can still get all the groupings (The $ groups you refer to) within the match object as they are included as a GroupCollection. Like so:
int count = 0;
string text = Regex.Replace(text,
#"(((http|ftp|https):\/\/|www\.)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])?)", //Example expression. This one captures URLs.
match =>
{
string replacementValue = String.Format("<a href='{0}'>{0}</a>", match.Value);
count++;
foreach (Group g in match.Groups)
{
g.Value; //Do stuff with g.Value
}
return replacementValue;
});

Categories