This question already has answers here:
Is this a bug in .NET's Regex.Split?
(2 answers)
Closed 9 years ago.
I have the following input:
void Main()
{
string inputData = "37.7879\r\n-122.3874\r\n40.7805\r\n-111.9288\r\n36.0667\r\n-115.0927\r\n37.7879\r\n-122.3874";
// string[] inputLines = Regex.Split(inputData, #"\r\n");
string[] inputLines = Regex.Split(inputData, #"(\r)?\n");
Console.WriteLine("The size of the list is: {0}", inputLines.Length);
bool results = inputLines.All(IsValidNumber);
foreach (string line in inputLines)
{
Console.WriteLine("{0} is: {1}", line, IsValidNumber(line));
}
}
// Define other methods and classes here
public bool IsValidNumber(string input)
{
Match match = Regex.Match(input, #"^-?\d+\.\d+$", RegexOptions.IgnoreCase);
return match.Success;
}
I am trying to a Regex.Split on #"\r\n", if I use the commented line, then I get the expected results. If I use the uncommented one, I do not get the results I expect. I'm almost 100% positive that my regex is correct if the "\r" doesn't exist (which may or may not be the case).
I'm expecting 8 values from inputData that I'm trying to validate if they're all valid numbers.
Is there a possibility that my "(\r)?" isn't working correctly? If so, what am I missing?
If your pattern contains a capturing group Regex.Split will capture the group as it's splitting the contents. This will give you 15 items instead of just 8.
If you're only trying to make a single character or character class optional, you don't need a group. Try getting rid of the group around \r:
string[] inputLines = Regex.Split(inputData, #"\r?\n");
Alternatively, yes, you can make it a non-capturing group:
string[] inputLines = Regex.Split(inputData, #"(?:\r)?\n");
Related
This question already has answers here:
How to get the digits before some particular word using regex in c#?
(8 answers)
Closed 2 years ago.
How to find the immediate integer value written before a string in c#? For example
50+ boxes were ordered, however only 2 are delivered.
I need to know the number of boxes (integer value) written just before "delivered". The output should be 2. I have written a code in c# using Regex:
string line = "50+ boxes were ordered, however only 2 are delivered.";
string boxesDelivered = Regex.Match(line, #"\d+").Value;
//The output I get is 50 instead of 2.
To get the last number that is followed by the word "delivered", you may use the following pattern:
\b\d+\b(?=[^\d]*\bdelivered\b)
Regex demo.
Here's a full example:
string line = "50+ boxes were ordered, however only 2 are delivered.";
var match = Regex.Match(line, #"\b\d+\b(?=[^\d]*\bdelivered\b)");
if (match.Success)
{
string boxesDelivered = match.Value;
// TODO: convert the value to a numeric type or use it as is.
}
Try it online.
written just before delivered
I'm going to take that verbatim as your user requirement - find the last number in the string that appears before "delivered".
You can use (\d+)[^\d]*(?:delivered), which says "match any sequence of numbers that does not occur before another sequence of numbers and does occur before delivered".
string line = "50+ boxes were ordered, however only 2 are delivered.";
string boxesDelivered = Regex.Match(line, #"(\d+)[^\d]*(?:delivered)").Groups[1].Value;
// boxesDelivered = 2
This question already has answers here:
Regular expression to extract text between square brackets
(15 answers)
Closed 3 years ago.
I need to split a string to extract the parentheses and data in a string array using a Regex and keep the parentheses as well.
Extract from
1-2-3(0)(1)
To
(0)
(1)
I constructed this Regex, but can't make it work.
String phrase= "123(0)(1)"
String[] results = Regex.Split(phrase,"\\r+(?:\\(.*\\))");
You can use Regex.Matches method instead
string phrase = "123(0)(1)";
string[] results = Regex.Matches(phrase, #"\(.*?\)").Cast<Match>().Select(m => m.Value).ToArray();
You could try using substring method if those two in parenthesis will always be together.
phrase = phrase.Substring(phrase.FirstIndexOf("("));
Might have to put -1 after.
You can extract the numbers in parentheses using (\(\d\)) pattern.
https://regex101.com/r/chjyLN/1
E.g.
var input = "1-2-3(0)(1)";
Regex pattern = new Regex(#"(\(\d\))");
var matches = pattern.Matches(input);
foreach (Match match in matches)
{
foreach (Capture capture in match.Captures)
{
Console.WriteLine(capture.Value);
}
}
This question already has answers here:
Using alternation and grouping constructs
(2 answers)
Closed 4 years ago.
I would like a match if the value contains either digits or matches the pattern [lookup('key')] where key can be any string.
Using either pattern on its own works. For example this works.
string value = "[lookup('anykey')]";
if (!Regex.IsMatch(value, "^\\[(lookup)\\(.*\\)\\]$"))
{
Console.WriteLine("no match");
}
I couldn't get both to work with a single regex.
if (!Regex.IsMatch(value, "((\\d+) | (\\[(parameter)\\(.*\\)\\]))"))
{
Console.WriteLine("no match");
}
Any idea?
In your regex you should remove spaces, try:
\d+|\[lookup\('[^']+'\)\]
First of all whenever I use regular expression in c# or for that matter any string that has characters that require escaping I prefix the string with '#' symbol, which saves me from using double escapes:)...I find it easier
Ok now to the answer, here is what I think you are looking for
static void Main(string[] args)
{
//string value = "[lookup('BlahBlah')]";
string value = "789897";
Match m = Regex.Match(value, #"((\d+)|(\[lookup\(\'([^\']+)\'\)\]))") ;
if (m.Success)
{
string num = m.Groups[2].Value;
string key = m.Groups[4].Value;
}
}
Notice how the string was prefixed with the '#' symbol and I didn't have to use double escapes for symbol \.
Since we are using so many parenthesis, we have 5 groups overall and if you want the number then you take the value of group 1, if you want the key the you take the value of group 4. If the 'num' is an empty string that means no number was supplied etc...
Hope that helps .....
This question already has answers here:
How do I extract text that lies between parentheses (round brackets)?
(19 answers)
Closed 7 years ago.
As I know for selecting a part of a string we use split. For example, if node1.Text is test (delete) if we choose delete
string b1 = node1.Text.Split('(')[0];
then that means we have chosen test, But if I want to choose delete from node1.Text how can I do?
Update:
Another question is that when there are two sets of parenthesis in the string, how one could aim at delete?. For example is string is test(2) (delete) - if we choose delete
You can also use regex, and then just remove the parentheses:
resultString = Regex.Match(yourString, #"\((.*?)\)").Value.
Replace("(", "").Replace(")", "");
Or better:
Regex.Match(yourString, #"\((.*?)\)").Groups[1].Value;
If you want to extract multiple strings in parentheses:
List<string> matches = new List<string>();
var result = Regex.Matches(yourString, #"\((.*?)\)");
foreach(Match x in result)
matches.Add(x.Groups[1].Value.ToString());
If your string is always xxx(yyy)zzz format, you can add ) character so split it and get the second item like;
var s = "test (delete) if we choose delete";
string b1 = s.Split(new[] { '(', ')' })[1];
string tmp = node1.Text.Split('(')[1];
string final = tmp.Split(')')[0];
Is also possible.
With the index [x] you target the part of the string before and after the character you have split the original string at. If the character occurs multiple times, your resulting string hat more parts.
I've been trying to do this for quite some time but for some reason never got it right.
There will be texts like these:
12325 NHGKF
34523 KGJ
29302 MMKSEIE
49504EFDF
The rule is there will be EXACTLY 5 digit number (no more or less) after that a 1 SPACE (or no space at all) and some text after as shown above. I would like to have a MATCH using a regex pattern and extract THE NUMBER and SPACE and THE TEXT.
Is this possible? Thank you very much!
Since from your wording you seem to need to be able to get each component part of the input text on a successful match, then here's one that'll give you named groups number, space and text so you can get them easily if the regex matches:
(?<number>\d{5})(?<space>\s?)(?<text>\w+)
On the returned Match, if Success==true then you can do:
string number = match.Groups["number"].Value;
string text = match.Groups["text"].Value;
bool hadSpace = match.Groups["space"] != null;
The expression is relatively simple:
^([0-9]{5}) ?([A-Z]+)$
That is, 5 digits, an optional space, and one or more upper-case letter. The anchors at both ends ensure that the entire input is matched.
The parentheses around the digits pattern and the letters pattern designate capturing groups one and two. Access them to get the number and the word.
string test = "12345 SOMETEXT";
string[] result = Regex.Split(test, #"(\d{5})\s*(\w+)");
You could use the Split method:
public class Program
{
static void Main()
{
var values = new[]
{
"12325 NHGKF",
"34523 KGJ",
"29302 MMKSEIE",
"49504EFDF"
};
foreach (var value in values)
{
var tokens = Regex.Split(value, #"(\d{5})\s*(\w+)");
Console.WriteLine("key: {0}, value: {1}", tokens[1], tokens[2]);
}
}
}