Quantity of specific strings inside a string - c#

I'm working in .net c# and I have a string text = "Whatever text FFF you can FFF imagine";
What i need is to get the quantity of times the "FFF" appears in the string text.
How can i acomplished that?
Thank you.

You can use regular expressions for this and right about anything you want:
string s = "Whatever text FFF you can FFF imagine";
Console.WriteLine(Regex.Matches(s, Regex.Escape("FFF")).Count);

Here are 2 approaches. Note that the regex should use the word boundary \b metacharacter to avoid incorrectly matching occurrences within other words. The solutions posted so far do not do this, which would incorrectly count "FFF" in "fooFFFbar" as a match.
string text = "Whatever text FFF you can FFF imagine fooFFFbar";
// use word boundary to avoid counting occurrences in the middle of a word
string wordToMatch = "FFF";
string pattern = #"\b" + Regex.Escape(wordToMatch) + #"\b";
int regexCount = Regex.Matches(text, pattern).Count;
Console.WriteLine(regexCount);
// split approach
int count = text.Split(' ').Count(word => word == "FFF");
Console.WriteLine(count);

Regex.Matches(text, "FFF").Count;

Use the System.Text.RegularExpressions.Regex for this:
string p = "Whatever text FFF you can FFF imagine";
var regex = new System.Text.RegularExpressions.Regex("FFF");
var instances = r.Matches(p).Count;
// instances will now equal 2,

Here's an alternative to the regular expressions:
string s = "Whatever text FFF you can FFF imagine FFF";
//Split be the number of non-FFF entries so we need to subtract one
int count = s.Split(new string[] { "FFF" }, StringSplitOptions.None).Count() - 1;
You could easily tweak this to use several different strings if necessary.

Related

Regex for text between two characters

I'm trying to get some text between two strings in C# in regex expression.
The text is in variable (tb1.product_name) : Example Text | a:10,Colour:Green
Get all text before |, in this case, Example Text
Get all text between : and ,, in this case, 10
In two differents regex.
I try with:
Regex.Match(tb1.product_name, #"\:([^,]*)\)").Groups[1].Value
But this doesn't work.
If it is not so necessary to use regex, you can do this simply by using string.Substring & string.IndexOf:
string str = "Example Text | a:10,Colour:Green";
string strBeforeVerticalBar = str.Substring(0, str.IndexOf('|'));
string strInBetweenColonAndComma = str.Substring(str.IndexOf(':') + 1, str.IndexOf(',') - str.IndexOf(':') - 1);
Edit 1:
I feel Regex might be an overkill for something as simple as this. Also if use what i suggested, you can add Trim() at the end to remove whitespaces, if any. Like:
string strBeforeVerticalBar = str.Substring(0, str.IndexOf('|')).Trim();
string strInBetweenColonAndComma = str.Substring(str.IndexOf(':') + 1, str.IndexOf(',') - str.IndexOf(':') - 1).Trim();
string str = #"Example Text |a:10,Colour: Green";
Match match = Regex.Match(str, #"^([A-Za-z\s]*)|$");
Match match2= Regex.Match(str, #":([0-9]*),");
//output Example Text
Console.WriteLine(match.Groups[1].Value);
//output 10
Console.WriteLine(match2.Groups[1].Value);

C# How to extract words from a string and put them into class members

I have a problem with c# string manipulation and I'd appreciate your help.
I have a file that contains many lines. It looks like this:
firstWord number(secondWord) thirdWord(Phrase) Date1 Date2
firstWord number(secondWord) thirdWord(Phrase) Date1 Time1
...
I need to separate these words and put them in a class properties. As you can see the problem is that the spaces between words are not the same, sometimes is one space sometimes eight spaces between them. And the second problem is that on the third place comes a phrase containing 2 to 5 words (again divided by spaces or sometimes contected with _ or -) and it needs to be considered as one string - it has to be one class member. The class should look like this:
class A
string a = firstWord;
int b = number;
string c = phrase;
Date d = Date1;
Time e = Time1;
I'd appreciate if you had any ideas how to solve this. Thank you.
Use the following steps:
Use File.ReadAllLines() to get a string[], where each element represents one line of the file.
For each line, use string.Split() and chop your line into individual words. Use both space and parentheses as your delimiters. This will give you an array of words. Call it arr.
Now create an object of your class and assign like this:
string a = arr[0];
int b = int.Parse(arr[1]);
string c = string.Join(" ", arr.Skip(4).Take(arr.Length - 6));
Date d = DateTime.Parse(arr[arr.Length - 2]);
Date e = DateTime.Parse(arr[arr.Length - 1]);
The only tricky stuff is string c above. Logic here is that from element no. 4 up to the 3rd last element, all of these elements form your phrase part, so we use linq to extract those elements and join them together to get back your phrase. This would obviously require that the phrase itself doesn't contain any parentheses itself, but that shouldn't normally be the case I assume.
You need a loop and string- and TryParse-methods:
var list = new List<ClassName>();
foreach (string line in File.ReadLines(path).Where(l => !string.IsNullOrEmpty(l)))
{
string[] fields = line.Trim().Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries);
if (fields.Length < 5) continue;
var obj = new ClassName();
list.Add(obj);
obj.FirstWord = fields[0];
int number;
int index = fields[1].IndexOf('(');
if (index > 0 && int.TryParse(fields[1].Remove(index), out number))
obj.Number = number;
int phraseStartIndex = fields[2].IndexOf('(');
int phraseEndIndex = fields[2].LastIndexOf(')');
if (phraseStartIndex != phraseEndIndex)
{
obj.Phrase = fields[2].Substring(++phraseStartIndex, phraseEndIndex - phraseStartIndex);
}
DateTime dt1;
if(DateTime.TryParse(fields[3], out dt1))
obj.Date1 = dt1;
DateTime dt2;
if (DateTime.TryParse(fields[3], out dt2))
obj.Date2 = dt2;
}
The following regular expression seems to cover what I imagine you would need - at least a good start.
^(?<firstWord>[\w\s]*)\s+(?<secondWord>\d+)\s+(?<thirdWord>[\w\s_-]+)\s+(?<date>\d{4}-\d{2}-\d{2})\s+(?<time>\d{2}:\d{2}:\d{2})$
This captures 5 named groups
firstWord is any alphanumeric or whitespace
secondWord is any numeric entry
thirdWord any alphanumeric, space underscore or hyphen
date is any iso formatted date (date not validated)
time any time (time not validated)
Any amount of whitespace is used as the delimiter - but you will have to Trim() any group captures. It makes a hell of a lot of assumptions about your format (dates are ISO formatted, times are hh:mm:ss).
You could use it like this:
Regex regex = new Regex( #"(?<firstWord>[\w\s]*)\s+(?<secondWord>\d+)\s+(?<thirdWord>[\w\s_-]+)\s+(?<date>\d{4}-\d{2}-\d{2})\s+(?<time>\d{2}:\d{2}:\d{2})$", RegexOptions.IgnoreCase );
var match = regex.Match("this is the first word 123 hello_world 2017-01-01 10:00:00");
if(match.Success){
Console.WriteLine("{0}\r\n{1}\r\n{2}\r\n{3}\r\n{4}",match.Groups["firstWord"].Value.Trim(),match.Groups["secondWord"].Value,match.Groups["thirdWord"].Value,match.Groups["date"].Value,match.Groups["time"].Value);
}
http://rextester.com/LGM52187
You have to use Regex, you may have a look here as a starting point. so for example to get the first word you may use this
string data = "Example 2323 Second This is a Phrase 2017-01-01 2019-01-03";
string firstword = new Regex(#"\b[A-Za-z]+\b").Matches(data )[0]

How to extract specific number in a string surrounded by numbers and text C#

I am trying to extract specific number in a string with a format of "Q23-00000012-A14" I only wanted to get the numbers in 8 digit 00000000 the 12.
string rx = "Q23-00000012-A14"
string numb = Regex.Replace(rx, #"\D", "");
txtResult.Text = numb;
But im getting the result of 230000001214, I only want to get the 12 and disregard the rest. Can someone guide me.
If your string are always in this format (numbers are covered with "-"), I suggest useing string.split()
string rx = "Q23-00000012-A14"
string numb = int.parse(rx.Split('-')[1]).ToString();//this will get 12 for you
txtResult.Text = numb;
It's an easier way than using regex
Edit!! When you use rx.split('-') , it break string into array of strings with value of splited texts before and after '-'
So in this case:
rx.Split('-')[0]= "Q23"
rx.Split('-')[1]= "00000012"
rx.Split('-')[2]= "A12"
So you shouldn't use Replace. Use Match instead.
string pattern = #"[A-Z]\d+-(\d+)-[A-Z]\d+" ;
var regex = new Regex(pattern);
var match = regex.Match("Q23-00000012-A14");
if (match.Success)
{
String eightNumberString = match.Groups[1].Value; // Contains "00000012"
int yourvalueAsInt = Convert.ToInt32(eightNumberString) ; // Contains 12
}
Why you use don't simply substring or split function ?
string rx = "Q23-00000012-A14";
// substring
int numb = int.Parse(rx.Substring(5, 8));
// or split
int numb = int.Parse(rx.Split('-')[1]);
txtResult.Text = numb.ToString();
(I think it's a better way to use split method because if you change your constant 'Q23' length the method still work)

Search string pattern

If I have a string like MCCORMIC 3H R Final 08-26-2011.dwg or even MCCORMIC SMITH 2N L Final 08-26-2011.dwg and I wanted to capture the R in the first string or the L in the second string in a variable, what is the best method for doing so? I was thinking about trying the below statement but it does not work.
string filename = "MCCORMIC 3H R Final 08-26-2011.dwg"
string WhichArea = "";
int WhichIndex = 0;
WhichIndex = filename.IndexOf("Final");
WhichArea = filename.Substring(WhichIndex - 1,1); //Trying to get the R in front of word Final
Just split by space:
var parts = filename.Split(new [] {' '},
StringSplitOptions.RemoveEmptyEntries);
WhichArea = parts[parts.Length - 3];
It looks like the file names have a very specific format, so this will work just fine.
Even with any number of spaces, using StringSplitOptions.RemoveEmptyEntries means spaces will not be part of the split result set.
Code updated to deal with both examples - thanks Nikola.
I had to do something similar, but with Mirostation drawings instead of Autocad. I used regex in my case. Here's what I did, just in case you feel like making it more complex.
string filename = "MCCORMIC 3H R Final 08-26-2011.dwg";
string filename2 = "MCCORMIC SMITH 2N L Final 08-26-2011.dwg";
Console.WriteLine(TheMatch(filename));
Console.WriteLine(TheMatch(filename2));
public string TheMatch(string filename) {
Regex reg = new Regex(#"[A-Za-z0-9]*\s*([A-Z])\s*Final .*\.dwg");
Match match = reg.Match(filename);
if(match.Success) {
return match.Groups[1].Value;
}
return String.Empty;
}
I don't think Oded's answer covers all cases. The first example has two words before the wanted letter, and the second one has three words before it.
My opinion is that the best way to get this letter is by using RegEx, assuming that the word Final always comes after the letter itself, separated by any number of spaces.
Here's the RegEx code:
using System.Text.RegularExpressions;
private string GetLetter(string fileName)
{
string pattern = "\S(?=\s*?Final)";
Match match = Regex.Match(fileName, pattern);
return match.Value;
}
And here's the explanation of RegEx pattern:
\S(?=\s*?Final)
\S // Anything other than whitespace
(?=\s*?Final) // Positive look-ahead
\s*? // Whitespace, unlimited number of repetitions, as few as possible.
Final // Exact text.

Getting a value from a string using regular expressions?

I have a string "Page 1 of 15".
I need to get the value 15 as this value could be any number between 1 and 100. I'm trying to figure out:
If regular expressions are best suited here. Considering string will never change maybe just split the string by spaces? Or a better solution.
How to get the value using a regular expression.
Regular expression you can use: Page \d+ of (\d+)
Regex re = new Regex(#"Page \d+ of (\d+)");
string input = "Page 1 of 15";
Match match = re.Match(input);
string pages = match.Groups[1].Value;
Analysis of expression: between ( and ) you capture a group. The \d stands for digit, the + for one or more digits. Note that it is important to be exact, so copy the spaces as well.
The code is a tad verbose, but I figured it'd be better understandable this way. With a split you just need: var pages = input.Split(' ')[3];, looks easier, but is error-prone. The regex is easily extended to grab other parts of the string in one go.
var myString = "Page 1 of 15";
var number = myString.SubString(myString.LastIndexOf(' ') + 1);
If there is a risk of whitespace at the end of the string then apply a TrimEnd method:
var number = myString.SubString(myString.TrimEnd().LastIndexOf(' ') + 1);
I think a simple String.Replace() is the best, most readable solution for something so simple.
string myString = "Page 1 of 15";
string pageNumber = myString.Replace("Page 1 of ", "");
EDIT:
The above solution assumes that the string will never be Page 2 of 15. If the first page number can change, you'll need to use String.Split() instead.
string myString = "Page 1 of 15";
string pageNumber = myString.Split(new string[] {"of"},
StringSplitOptions.None).Last().Trim();
if the string format will never change then you can try this...
string input = "Page 1 of 15";
string[] splits = input.Split(' ');
int totalPages = int.Parse(splits[splits.Length - 1]);
If this is the only case you will ever have to handle, just split the string by spaces and use the parse the 4th part to an integer. Something like that will work:
string input = "Page 1 of 15";
string[] splitInput = string.Split(' ');
int numberOfPages = int.Parse(splitInput[3]);
In c# it should looks like this (using Regex class):
Regex r = new Regex(#"Page \d+ of (\d+)");
var str = "Page 1 of 15";
var mathes = r.Matches(str);
Your resoult will be in: mathes[0].Groups[1]
You dont need a regex for this. Just the the index of the last space
string var = "1 of100";
string secondvar = string.Empty;
int startindex = var.LastIndexOf(" ");
if (startindex > -1)
{
secondvar = var.Substring(startindex +1);
}

Categories

Resources