I search in a text for some strings and want to remove the first and last char in those strings.
Example :
...
...
OK 125 ab_D9 "can be "this" or; can not be "this" ";
...
OK 673 e_IO1_ "hello; is strong
or maybe not strong";
...
So I use the code to find all strings begin with OK and remove from the 4 groups "...":
tmp = fin.ReadToEnd();
var matches = Regex.Matches(tmp, "(OK) ([0-9]+) ([A-Za-z_0-9]+) (\"(?:(?!\";).)*\");", RegexOptions.Singleline);
for (int i = 0; i < matches.Count; i++)
{
matches[i].Groups[4].Value.Remove(0);
matches[i].Groups[4].Value.Remove(matches[i].Groups[4].Value.ToString().Length - 1);
Console.WriteLine(matches[i].Groups[1].Value + "\r\n" + "\r\n" + "\r\n" + matches[i].Groups[2].Value + "\r\n" + "\r\n" + matches[i].Groups[3].Value + "\r\n" + "\r\n" + "\r\n" + matches[i].Groups[4].Value);
Console.WriteLine(" ");
}
But it doesn't remove first and last char from Group 4. What did I do wrong?
My Result should be:
OK
125
ab_D9
can be "this" or; can not be "this"
OK
673
e_IO1
hello; is strong
or maybe not strong
There is no need to remove things. Just don't capture the quotes in the first place. So move the parentheses one character inward.
"(OK) ([0-9]+) ([A-Za-z_0-9]+) \"((?:(?!\";).)*)\";"
You should assign the result of Substring() and Remove() methods. they do not change the existing string but return the changed string which you need to assign to the same or some other string variable. Check the code:
tmp = fin.ReadToEnd();
var matches = Regex.Matches(tmp, "(OK) ([0-9]+) ([A-Za-z_0-9]+) (\"(?:(?!\";).)*\");", RegexOptions.Singleline);
for (int i = 0; i < matches.Count; i++)
{
string str = matches[i].Groups[4].Value.Substring(0);
str = str.Remove(str.Length - 1);
Console.WriteLine(matches[i].Groups[1].Value + "\r\n" + "\r\n" + "\r\n" + matches[i].Groups[2].Value + "\r\n" + "\r\n" + matches[i].Groups[3].Value + "\r\n" + "\r\n" + "\r\n" + str);
Console.WriteLine(" ");
}
P.S. You should use Environment.NewLine instead of "\r\n", it's the better approach.
Related
I have a list of names and I loop through them to create a comma separated list in a string variable (Bob, George, Will, Terry).
I need the list to eventually look like (Bob, George, Will and Terry).
How do I find the LAST instance of the comma and replace it with the word "and"? Once I find the LAST instance, I think it's a simple matter of doing something like
string new=ori.Substring(0,start) + rep + ori.Substring(start+rep.Length);
Thoughts? Comments? Suggestions?
Thanks,
Bob
This should work for you. Added the alternative comma style as well.
var names = "Bob, George, Will, Terry";
var lastCommaPosition = names.LastIndexOf(',');
if (lastCommaPosition != -1)
{
names = names.Remove(lastCommaPosition, 1)
//.Insert(lastComma, " and");
.Insert(lastCommaPosition, ", and");
}
Console.WriteLine(names);
You can use a combination of LINQ and String.Join. This solution does not need the last index of a comma and is "more fluent" to read.
var list = new List<string> { "Bob", "George", "Will", "Terry" };
var listAsString = list.Count > 1
? string.Join(", ", list.Take(list.Count - 1)) + " and " + list.Last()
: list.First();
You can use Linq,
list.Select(i => i).Aggregate((i, j) => i + (list.IndexOf(j) == list.Count -1 ? " and " : " , ") + j);
Hope helps,
This should do the trick for you:
var foo = "Bob, George, Will, Terry";
if (foo.Contains(",")) {
foo = foo.Substring(0, foo.LastIndexOf(",")) + " and" + foo.Substring(foo.LastIndexOf(",")+ 1);
}
I'm not sure what you wanted to do, but the following code works:
string original = "(Bob, George, Will, Terry)";
string result = "";
string[] splited = original.Split(',');
for (int i = 0; i < splited.Count(); i++)
{
if(i == splited.Count() - 2)
{
result += splited[i] + " and";
}
else if(i == splited.Count() - 1)
{
result += splited[i];
}
else
{
result += splited[i] + ",";
}
}
I Used split to split the original string in a vector so i worked with this vector to replace the last comma to the word "and".
I'm only getting the last entry of the counting typed like this
public string ZodziuSkaiciavimas()
{
foreach (var sentence in Sakiniai.TrimEnd('.').Split('.'))
{
Rezultatas=(eilute.ToString() + " sakinyje zodziu:" + (sentence.Trim().Split(' ').Count() + sentence.Trim().Split('-').Count() + sentence.Trim().Split(';').Count() + sentence.Trim().Split(':').Count() + sentence.Trim().Split(',').Count() - 4));
eilute++;
}
return Rezultatas;
And I need to get the answer with a return type.
If I type code like this than i get what i want,but no returns.
public string ZodziuSkaiciavimas()
{
foreach (var sentence in Sakiniai.TrimEnd('.').Split('.'))
{
Console.WriteLine(eilute.ToString() + " sakinyje zodziu:" + (sentence.Trim().Split(' ').Count() + sentence.Trim().Split('-').Count() + sentence.Trim().Split(';').Count() + sentence.Trim().Split(':').Count() + sentence.Trim().Split(',').Count() - 4));
eilute++;
}
return Rezultatas;
}
Why arent you appending your results as below
Rezultatas +=(eilute.ToString() + " sakinyje zodziu:" + (sentence.Trim().Split(' ').Count() + sentence.Trim().Split('-').Count() + sentence.Trim().Split(';').Count() + sentence.Trim().Split(':').Count() + sentence.Trim().Split(',').Count() - 4)) + "\n";
It looks like you want to return multiple numbers from your method, but Rezultatas is a single string. You can fix it by changing the return type to List<int>, and returning a list:
public List<int> ZodziuSkaiciavimas() {
var Rezultatas = new List<int>()
foreach (var sentence in Sakiniai.TrimEnd('.').Split('.')) {
var res = sentence.Trim().Split(' ', '-', ';', ':', ',').Length;
Rezultatas.Add(res);
}
return Rezultatas;
}
When the callers decide to print the Rezultatas they gets back from your method, they could decide what character to put between the numbers (say, a comma ',') and print it like this:
var numbers = ZodziuSkaiciavimas();
Console.WriteLine(string.Join(", ", numbers));
I have this little method to look for the 3-digit number in a string and increment it by one. The types of strings I am passing in are like CP1-P-CP2-004-D and MOT03-C-FP04-003.
char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
foreach (char c in alphabet)
{
m = Regex.Match(s, #"\d{3}(?=[" + c + "-]|$)");
}
if (m.Success)
{
int i = Convert.ToInt32(m.Value); i += 1;
Console.WriteLine(s + " - " + i.ToString("D3"));
}
else { Console.WriteLine(s + " - No success"); }
EDIT: Initially I just had this; to test out my Regex.Match case:
Match m = Regex.Match(s, #"\d{3}(?=[A-]|$)");
And it worked with CP1PCP2001A no worries, but when I updated it, and tried CP1PCP2001C it returned "No Success", while CP1PCP2001 works no problem. Can anyone tell me why this is?
Have you tried
m = Regex.Match(s, #"\d{3}(?=[A-Z\-]|$)");
[A-Z] means that it can be any of the capital letters between A and Z thus eliminating the need for char[] alphabet, and the \- allows you to add the '-' as a parameter, without causing conflict with the first parameter.
From the comments, we're looking for "the first 3 digit number (coming from the right)". Here's a literal implementation:
m = Regex.Match(s, #"\d{3}", RegexOptions.RightToLeft);
This is more permissive towards unexpected characters than the other answers. You can decide whether that's good or bad for your application.
re-write the code this way
bool matched = false;
foreach (char c in alphabet)
{
m = Regex.Match(s, #"\d{3}(?=[" + c + "-]|$)");
if (m.Success)
{
int i = Convert.ToInt32(m.Value); i += 1;
Console.WriteLine(s + " - " + i.ToString("D3"));
matched=true;
break;
}
}
if(!matched)
Console.WriteLine(s + " - No success");
a better way would be not to loop and specify the char range to match in regex itself
example
m = Regex.Match(s, #"\d{3}(?=[A-Z\-]|$)");
if (m.Success)
{
int i = Convert.ToInt32(m.Value); i += 1;
Console.WriteLine(s + " - " + i.ToString("D3"));
}
else
Console.WriteLine(s + " - No success");
regex demo here
i need to split a text file, with values separated by comma and with text qualifier like ¨|¨
I was trying to use these function:
public string[] Split(string expression, string delimiter,
string qualifier, bool ignoreCase)
{
string _Statement = String.Format
("{0}(?=(?:[^{1}]*{1}[^{1}]*{1})*(?![^{1}]*{1}))",
Regex.Escape(delimiter), Regex.Escape(qualifier));
RegexOptions _Options = RegexOptions.Compiled | RegexOptions.Multiline;
if (ignoreCase) _Options = _Options | RegexOptions.IgnoreCase;
Regex _Expression = new Regex(_Statement, _Options);
return _Expression.Split(expression);
}
to process a text file with rows like this one:
¨|¨column 1¨|¨,¨|¨column 2¨|¨,¨|¨column 3¨|¨,¨|¨column 4¨|¨
But my regex expression is not working...
Any ideas that could help me to make this work?
Thanks in advance
You can do this without a Regex, just split the string by ¨|¨ then each item by a space to get the individual key/value e.g.
foreach (var item in str.Split(new[] { "¨|¨" }, StringSplitOptions.RemoveEmptyEntries))
{
var tokens = item.Split(' ');
Console.WriteLine(tokens[0]);
Console.WriteLine(tokens[1]);
}
Not really sure why you need Regex for something like this, string.Split can give you the output you need like:
string str = "¨|¨column 1¨|¨,¨|¨column 2¨|¨,¨|¨column 3¨|¨,¨|¨column 4¨|¨";
string[] splitArray = str.Split(new[] { "¨|¨,", "¨|¨" }
, StringSplitOptions.RemoveEmptyEntries);
For output:
foreach (var item in splitArray)
{
Console.WriteLine(item);
}
Output:
column 1
column 2
column 3
column 4
In .net, we can do this! :)
I just pushed through it and feel like sharing.
This is a pretty full regex solution to splitting a delimited file row:
private bool RowMe(string strColumnDelimiter, string strTextQualifier, string strInput, out string[] strSplitOutput, out string strResultMessage)
{
string[] retVal = null;
bool blnResult = false;
strResultMessage = "";
//---- We need to escape at least some of the most common
// special characters for both delimiter & qualifier ----
switch (strColumnDelimiter) {
case "|":
strColumnDelimiter = "\\|";
break;
case "\\":
strColumnDelimiter = "\\\\";
break;
}
switch (strTextQualifier)
{
case "\"":
strTextQualifier = "\\\"";
break;
}
//---- Let's have our delimited row splitter regex! ----
string strPattern = String.Concat(
"^"
,"(?:"
,"("
, "[^\\S" + strColumnDelimiter + strTextQualifier + "]*" // allow leading whitespace, not counting our delimiter & qualifier
,"(?:"
,"(?:[^" + strColumnDelimiter + strTextQualifier +"]*)" // any amount of characters not colum-delimiter or text-qualifier
,"|"
, "(?:" + strTextQualifier + "(?:(?:[^" + strTextQualifier + "])|(?:" + strTextQualifier + strTextQualifier + "))*" + strTextQualifier + ")" // any amount of characters not text-qualifier OR doubled-text-qualifier inside leading & trailing text-qualifier (allow even colum-delimiter inside text qualifier)
,"|"
,"(?:(?:[^" + strColumnDelimiter + strTextQualifier + "]{1})(?:[^" + strColumnDelimiter + "]*)(?:[^" + strColumnDelimiter + strTextQualifier + "]{1}))" // any amount of characters not column-delimiter inside other leading & trailing characters not column-delimiter or text-qualifier (allow text-qualifier inside value if it is not leading or trailing)
,")"
, "[^\\S" + strColumnDelimiter + strTextQualifier + "]*" // allow trailing whitespace, not counting our delimiter & qualifier
,")"
, "){0,1}"
//-- note how this second section is almost the same as the first but with a leading delimiter...
// the first column must not have a leading delimiter, and any subsequent ones must
, "(?:"
,"(?:"
, strColumnDelimiter // << :)
,"(?:"
, "("
, "[^\\S" + strColumnDelimiter + strTextQualifier + "]*" // allow leading whitespace, not counting our delimiter & qualifier
, "(?:"
, "(?:[^" + strColumnDelimiter + strTextQualifier + "]*)" // any amount of characters not colum-delimiter or text-qualifier
, "|"
, "(?:" + strTextQualifier + "(?:(?:[^" + strTextQualifier + "])|(?:" + strTextQualifier + strTextQualifier + "))*" + strTextQualifier + ")" // any amount of characters not text-qualifier OR doubled-text-qualifier inside leading & trailing text-qualifier (allow even colum-delimiter inside text qualifier)
, "|"
, "(?:(?:[^" + strColumnDelimiter + strTextQualifier + "]{1})(?:[^" + strColumnDelimiter + "]*)(?:[^" + strColumnDelimiter + strTextQualifier + "]{1}))" // any amount of characters not column-delimiter inside other leading & trailing characters not column-delimiter or text-qualifier (allow text-qualifier inside value if it is not leading or trailing)
, ")"
, "[^\\S" + strColumnDelimiter + strTextQualifier + "]*" // allow trailing whitespace, not counting our delimiter & qualifier
, ")"
,")"
,")"
, "){0,}"
,"$"
);
);
//---- And do the regex Match-ing ! ----
System.Text.RegularExpressions.Regex objRegex = new System.Text.RegularExpressions.Regex(strPattern);
System.Text.RegularExpressions.MatchCollection objMyMatches = objRegex.Matches(strInput);
//---- So what did we get? ----
if (objMyMatches.Count != 1) {
blnResult = false;
strResultMessage = "--NO-- no overall match";
}
else if (objMyMatches[0].Groups.Count != 3) {
blnResult = false;
strResultMessage = "--NO-- pattern not correct";
throw new ApplicationException("ERROR SPLITTING FLAT FILE ROW! The hardcoded regular expression appears to be broken. This should not happen!!! What's up??");
}
else {
int cnt = (1 + objMyMatches[0].Groups[2].Captures.Count);
retVal = new string[cnt];
retVal[0] = objMyMatches[0].Groups[1].Captures[0].Value;
for (int i = 0; i < objMyMatches[0].Groups[2].Captures.Count; i++) {
retVal[i+1] = objMyMatches[0].Groups[2].Captures[i].Value;
}
blnResult = true;
strResultMessage = "SUCCESS";
}
strSplitOutput = retVal;
return blnResult;
}
How can I count number of rows in specified column in a Excel sheet?
For example I have 2 columns in a spreadsheet:
A B
--- -----
abc hi
fff hello
ccc hi
hello
The result should look like:
count of A column is 3
count of B column is 4
How can I do this using Microsoft Interop?
The approach suggested by Doug Glancy is accurate and simple to be implemented. You can write the function and retrieve the value from a cell not seenable by the user (ZZ1000, for example). The code is straightforward:
Range notUsed = curSheet.get_Range("ZZ1000", "ZZ1000");
string targetCol = "A";
notUsed.Value2 = "=COUNTA(" + targetCol + ":" + targetCol + ")";
int totRows = Convert.ToInt32(notUsed.Value2);
notUsed.Value2 = "";
UPDATE ---
From your example I understood that you were looking for the total number of non-empty cells, what COUNTA delivers. But, apparently, this is not the case: you want the row number of the last non-empty cell; that is, by using a more descriptive example:
C
---
abc
fff
ccc
hello
You don't want to count the number of non-empty cells (4 in this case; what COUNTA delivers), but the position of "hello", that is, 5.
I don't like relying on Excel formulae too much, unless for clearly-defined problems (like yours, as I understood it initially). Excel formulae deliver still the best solution for what you really want (although its complexity is right "in the limit"). To account for the situation as described above, you can rely on MATCH. If your cells contain text (at least one letter per cell), the code can be changed into:
notUsed.Value2 = "=MATCH(REPT(\"z\",255)," + targetCol + ":" + targetCol + ")";
In case of having numeric values (not a single letter in the cell):
notUsed.Value2 = "=MATCH(LOOKUP(" + Int32.MaxValue.ToString() + "," + targetCol + ":" + targetCol + ")," + targetCol + ":" + targetCol + ")";
If you want to account for both options, you would have to combine these equations: you can create a new formula including both; or you might rely on C# code (e.g., get the values from both equations and consider only the bigger one).
Bear also in mind that you have to account for cases where no matches are found. Here you have a code accounting for both situations (letters and numbers via C# code) and for no matches:
notUsed.Value2 = "=MATCH(REPT(\"z\",255)," + targetCol + ":" + targetCol + ")";
int lastLetter = Convert.ToInt32(notUsed.Value2);
if (lastLetter == -2146826246)
{
lastLetter = 0;
}
totRows = lastLetter;
notUsed.Value2 = "=MATCH(LOOKUP(" + Int32.MaxValue.ToString() + "," + targetCol + ":" + targetCol + ")," + targetCol + ":" + targetCol + ")";
int lastNumber = Convert.ToInt32(notUsed.Value2);
if (lastNumber == -2146826246)
{
lastNumber = 0;
}
if (lastNumber > totRows)
{
totRows = lastNumber;
}
This should do it:
private static int GetRowsInColumnOnWorkSheetInWorkbook(string workbookName, int worksheetNumber, int workSheetColumn)
{
return new Excel.Application().Workbooks.Open(workbookName)
.Sheets[worksheetNumber]
.UsedRange
.Columns[workSheetColumn]
.Rows
.Count;
}
You could have the following override also:
private static int GetRowsInColumnOnWorkSheetInWorkbook(string workbookName, string worksheetName, int workSheetColumn)
{
return new Excel.Application().Workbooks.Open(workbookName)
.Sheets[worksheetName]
.UsedRange
.Columns[workSheetColumn]
.Rows
.Count;
}
It's slightly longer than the other answer, but I think this is more readable, and simpler.