I need to get all numbers from a string like this:
"156234 something 567345 another thing 45789 anything"
The result should be a collection of numbers having:
156234, 567345, 45789
I tried #"\d+", but it will only give me 156234.
EDIT: The numbers are integers, however they can also occur like this "156234 something 567345 another thing 45789 anything2345". In this case I only need the integers i.e 156234, 567345, 45789 and not 156234, 567345, 45789,2345.
Also the integers which i dont want will always be preceed with a text for ex:anything2345.
Everything is ok with your regex, you just need to come through all the matches.
Regex regex = new Regex(#"\d+");
foreach (Match match in regex.Matches("156234 something 567345 another thing 45789 anything"))
{
Console.WriteLine(match.Value);
}
You want to split on the characters, not the digits. Use the capital D
string[] myStrings = Regex.Split(sentence, #"\D+");
Source
If 2345 should not be matched in your revised sample string (156234 something 567345 another thing 45789 anything2345), you could use Dima's solution but with the regex:
\b\d+\b
This assures that the number is surrounded by word boundaries.
This'z in java. You don't actually need a Regex.. just a normal replaceAll should do the trick for you! : For ex : You can strip off the Non-Digits, and then split and calculate the sum.
public static int getSumOfNumbers(String s) {
int sum = 0;
String x = s.replaceAll("\\D+", " ");
String[] a = x.split(" ");
for(int i = 0; i < a.length; i++)
sum += Integer.parseInt(a[i]);
System.out.println(x + " : and sum is : "+sum);
return sum;
}
Related
I have a string that I need to separate the product ID from, is this format
shop:?id:556:token:bmgwcGJxZEpnK2RqemhaKzdBYWZjbTVZN0xaOXh5L3pmdDBFZjQrWVVES1pmYVBXVVB6SlFhejBsNndnaHNsUA==
I need to get 556 out of there, and in the case of say 2658 etc also possible.
First index ":" I think
str.Substring(str.LastIndexOf(':') + 1);
But then I dont know how to just break after the match, regex better? any help apprecaited
EDIT
These do the exact same thing, seperating the first numbers out
LINQ:
var test = new string(str.Substring(str.IndexOfAny("0123456789".ToCharArray())).TakeWhile(char.IsDigit).ToArray());
Reggex:
var test = Regex.Match(str, #"\d+").Value;
So bears the question, which is better approach?
If the string format is fixed, use the Split function
string str = "shop:?id:556:token:bmgwcGJxZEpnK2RqemhaKzdBYWZjbTVZN0xaOXh5L3pmdDBFZjQrWVVES1pmYVBXVVB6SlFhejBsNndnaHNsUA==";
int id = Convert.ToInt32(str.Split(':')[2]);
Console.WriteLine(id);
I'd probably use Regex:
var id = Regex.Match(input, #"\?id:(?<x>\d+)").Groups["x"].Value
Decoded, that Regex means "literally match ?id: then start a capturing group called x and capture one or more digits into it"
The returned Match will have a Groups property that we index by x and retrieve the value
If you want it as an int you can int.Parse the result-you won't need a TryParse because the Regex will have only matched digits
If the format of the string is fixed then this would work:
input[9..input.IndexOf(':',10)];
And it would be more performant than Regex or Split
If you wanted a substring that works with a format change, perhaps:
var x = input.IndexOf("?id:") + 4;
var id = input[x..input.IndexOf(':',x+1)];
This will work even if the order of items changes.
string original = "shop:?id:556:token:bmgwcGJxZEpnK2RqemhaKzdBYWZjbTVZN0xaOXh5L3pmdDBFZjQrWVVES1pmYVBXVVB6SlFhejBsNndnaHNsUA==";
string startWithId = original.Substring(original.IndexOf("id:") + 3);
string onlyId = startWithId.Split(':')[0];
Console.WriteLine(onlyId);
I have strings of the following form:
str = "[int]:[int],[int]:[int],[int]:[int],[int]:[int], ..." (for undefined number of times).
What I did was this:
string[] str_split = str.Split(',');
for( int i = 0; i < str_split.Length; i++ )
{
string[] str_split2 = str_split[i].Split(':');
}
Unfortunately this breaks when some of the numbers have extra ',' inside a number. For example, we have something like this:
695,000:14,306,000:12,136000:12,363000:6
in which the followings are the numbers, ordered from the left to the right:
695,000
14
306,000
12
136000
12
363000
6
How can I resolve this string splitting problem?
If it is the case that only the number to the left of the colon separator can contain commas, then you could simply express this as:
string s = "695,000:14,306,000:12,136000:12,363000:6";
var parts = Regex.Split(s, #":|(?<=:\d+),");
The regex pattern, which identifies the separators, reads: "any colon, or any comma that follows a colon and a sequence of digits (but not another comma)".
A simple solution is split using : as delimiter. The resultant array will have numbers of the format [int],[int]. Parse through the array and split each entry using , as the delimiter. This will give you an array of [int] numbers.
It might not be the best way to do it and it might not work all the time but here's what I'd do.
string[] leftRightDoubles = str.Split(':');
foreach(string substring in leftRightDoubles){
string[] indivNumbers = str.Split(',');
//if indivNumbers.Length == 2, you know that these two are separate numbers
//if indivNumbers.Length > 2, use heuristics to determine which parts belong to which number
if(indivNumbers.Length > 2) {
for(int i = 0, i < indivNumbers.Length, i++) {
if(indivNumbers[i] != '000') { //Or use some other heuristic
//It's a new number
} else {
//It's the rest of previous number
}
}
}
}
//It's sort of pseudocode with comments (haven't touched C# in a while so I don't want to write full C# code)
How To Convert The String (X LE) To INT (X)
X = Number
I used :
Convert.ToInt32(Form1.sendproductprice1)*Convert.ToInt32(Form1.sendamount));
Example :
Form1.sendproductprice1 = "25 LE";
Form1.sendamount = 5;
Then value must be 125
But I got Error "Input string was not in correct format"
Obviously, 25 LE can't be converted to integer like that. You have to separate the number from the text. In this case, you can use
var num = Form1.sendproductprice1.Split(' ')[0];
which basically takes your input, splits it by spaces and takes the first item from the result. Then this will work
Convert.ToInt32(num)*Convert.ToInt32(Form1.sendamount));
Code below should be work:
Convert.ToInt32(Form1.sendproductprice1.Split(' ')[0])*Convert.ToInt32(Form1.sendamount));
You can extract the number from the string with this code (it allows you to extract the number even if it's not at the beggining)
for (int i=0; i< Form1.sendproductprice1.Length; i++)
{
if (Char.IsDigit(Form1.sendproductprice1[i]))
number += Form1.sendproductprice1[i];
}
Then if you do Convert.ToInt32(number) it will work just fine
You could also use regular expressions.
You will first need to separate the characters from the string to be able to convert the number to integer type.
Convert.ToInt32(Form1.sendproductprice1)
Will throw an exception if the string has something other than integers.
In your case (in the example you provided) the string is as follows: "25 LE"
If the delimeter is always a space then its easy:
var test = "25 LE";
var splitted = test.Split(' ');
var digits = splitted[0]; //Will get you the digits only.
If you want to handle whitespace you can use a Regex as well to parse the input
string input = " 25 LE ";
Regex regex = new Regex("\\d+");
Match match = regex.Match(input);
if (match.Success)
{
int number = Convert.ToInt32(match.Value); // number contains 25
}
I want to find number of letter "a" in only first sentence. The code below finds "a" in all sentences, but I want in only first sentence.
static void Main(string[] args)
{
string text; int k = 0;
text = "bla bla bla. something second. maybe last sentence.";
foreach (char a in text)
{
char b = 'a';
if (b == a)
{
k += 1;
}
}
Console.WriteLine("number of a in first sentence is " + k);
Console.ReadKey();
}
This will split the string into an array seperated by '.', then counts the number of 'a' char's in the first element of the array (the first sentence).
var count = Text.Split(new[] { '.', '!', '?', })[0].Count(c => c == 'a');
This example assumes a sentence is separated by a ., ? or !. If you have a decimal number in your string (e.g. 123.456), that will count as a sentence break. Breaking up a string into accurate sentences is a fairly complex exercise.
This is perhaps more verbose than what you were looking for, but hopefully it'll breed understanding as you read through it.
public static void Main()
{
//Make an array of the possible sentence enders. Doing this pattern lets us easily update
// the code later if it becomes necessary, or allows us easily to move this to an input
// parameter
string[] SentenceEnders = new string[] {"$", #"\.", #"\?", #"\!" /* Add Any Others */};
string WhatToFind = "a"; //What are we looking for? Regular Expressions Will Work Too!!!
string SentenceToCheck = "This, but not to exclude any others, is a sample."; //First example
string MultipleSentencesToCheck = #"
Is this a sentence
that breaks up
among multiple lines?
Yes!
It also has
more than one
sentence.
"; //Second Example
//This will split the input on all the enders put together(by way of joining them in [] inside a regular
// expression.
string[] SplitSentences = Regex.Split(SentenceToCheck, "[" + String.Join("", SentenceEnders) + "]", RegexOptions.IgnoreCase);
//SplitSentences is an array, with sentences on each index. The first index is the first sentence
string FirstSentence = SplitSentences[0];
//Now, split that single sentence on our matching pattern for what we should be counting
string[] SubSplitSentence = Regex.Split(FirstSentence, WhatToFind, RegexOptions.IgnoreCase);
//Now that it's split, it's split a number of times that matches how many matches we found, plus one
// (The "Left over" is the +1
int HowMany = SubSplitSentence.Length - 1;
System.Console.WriteLine(string.Format("We found, in the first sentence, {0} '{1}'.", HowMany, WhatToFind));
//Do all this again for the second example. Note that ideally, this would be in a separate function
// and you wouldn't be writing code twice, but I wanted you to see it without all the comments so you can
// compare and contrast
SplitSentences = Regex.Split(MultipleSentencesToCheck, "[" + String.Join("", SentenceEnders) + "]", RegexOptions.IgnoreCase | RegexOptions.Singleline);
SubSplitSentence = Regex.Split(SplitSentences[0], WhatToFind, RegexOptions.IgnoreCase | RegexOptions.Singleline);
HowMany = SubSplitSentence.Length - 1;
System.Console.WriteLine(string.Format("We found, in the second sentence, {0} '{1}'.", HowMany, WhatToFind));
}
Here is the output:
We found, in the first sentence, 3 'a'.
We found, in the second sentence, 4 'a'.
You didn't define "sentence", but if we assume it's always terminated by a period (.), just add this inside the loop:
if (a == '.') {
break;
}
Expand from this to support other sentence delimiters.
Simply "break" the foreach(...) loop when you encounter a "." (period)
Well, assuming you define a sentence as being ended with a '.''
Use String.IndexOf() to find the position of the first '.'. After that, searchin a SubString instead of the entire string.
find the place of the '.' in the text ( you can use split )
count the 'a' in the text from the place 0 to instance of the '.'
string SentenceToCheck = "Hi, I can wonder this situation where I can do best";
//Here I am giving several way to find this
//Using Regular Experession
int HowMany = Regex.Split(SentenceToCheck, "a", RegexOptions.IgnoreCase).Length - 1;
int i = Regex.Matches(SentenceToCheck, "a").Count;
// Simple way
int Count = SentenceToCheck.Length - SentenceToCheck.Replace("a", "").Length;
//Linq
var _lamdaCount = SentenceToCheck.ToCharArray().Where(t => t.ToString() != string.Empty)
.Select(t => t.ToString().ToUpper().Equals("A")).Count();
var _linqAIEnumareable = from _char in SentenceToCheck.ToCharArray()
where !String.IsNullOrEmpty(_char.ToString())
&& _char.ToString().ToUpper().Equals("A")
select _char;
int a =linqAIEnumareable.Count;
var _linqCount = from g in SentenceToCheck.ToCharArray()
where g.ToString().Equals("a")
select g;
int a = _linqCount.Count();
i'm trying to find out how i can get the first x Matches of a Char in a String. I tried using a Matchcollection but i cant find any escapesequence to stop after the x'd-match.
FYI:
I need this for a string with a variable length and a different number of occurences of the searched Char, so just getting all and using only the first x isnt a solution.
Thanks in advance
Edit:
I am using steam reader to get information out of a .txt files and write it to a atring, for each file one string. These atrings have very different lengths. In every string are lets say 3 keywords. But sometimes something went wrong and i have only one or two of the keywords. Between the keywords are other fields separated with a ;. So if i use a Matchcollection to get the indexes of the ;'s and one Keyword is missing the Information in the File is shifted. Because of that i need to find the first x occourencces before/after a (existing)keyword.
Do you really want to use Regex, something like this won't do ?
string simpletext = "Hello World";
int firstoccur = simpletext.IndexOfAny(new char[]{'o'});
Since you want all the indexes for that character you can try in this fashion
string simpletext = "Hello World";
int[] occurences = Enumerable.Range(0, simpletext.Length).Where(x => simpletext[x] == 'o').ToArray();
You can use the class Match. this class returns only one result, but you can iterate over the string till it found the last one.
Something like this:
Match match = Regex.Match(input, pattern);
int count = 0;
while (match.Success)
{
count++;
// do something with match
match = match.NextMatch();
// Exit the loop when your match number is reached
}
If you're determined to use Regex then I'd do this with Matches as opposed to Match actually; largely because you get the count up front.
string pattern = "a";
string source = "this is a test of a regex match";
int maxMatches = 2;
MatchCollection mc = Regex.Matches(source, pattern);
if (mc.Count() > 0)
{
for (int i = 0; i < maxMatches; i++)
{
//do something with mc[i].Index, mc[i].Length
}
}
The split operation is pretty fast so if the regex is not a requirement this could be used:
public static IEnumerable<int> IndicesOf(this string text, char value, int count)
{
var tokens = text.Split(value);
var sum = tokens[0].Length;
var currentCount = 0;
for (int i = 1; i < tokens.Length &&
sum < text.Length &&
currentCount < count; i++)
{
yield return sum;
sum += 1 + tokens[i].Length;
currentCount++;
}
}
executes in roughly 60% of the time of the regex