I have a input list that takes input in the above format and put them into a comma seperated string. I would like to get strings before and after colon(:).
I tried this regex pattern
string[] reg = Regex.Split(x, #"^(?:[\w ]\:\s[\w]+)+$");
but it doesnt seem to work. Please help.
Below is my code. This is a C# console application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
List<string> input = new List<string>();
Console.WriteLine("Please enter your input");
string readinput = Console.ReadLine();
input.Add(readinput);
while (readinput != "")
{
readinput = Console.ReadLine();
input.Add(readinput);
}
string x = string.Join(",", input.ToArray());
Console.WriteLine(x);
// using regex
string[] reg = Regex.Split(x, #"^(?:[\w ]\:\s[\w]+)+$");
Console.WriteLine(reg);
Console.ReadLine();
}
}
}
Sorry i was not very clear but the
input : Amby : Dexter,
Dexter : Karla,
Karla : Matt .....
Expected Output is Amby, Dexter, Karla, matt....
If I understood you correctly... User enters some strings, and then you join them with commas. After that you want to split that string by colons?
Why don't you use simpler solution like this:
string[] reg = x.Split(':').Select(s => s.Trim()).ToArray();
Maybe this will get you started:
new Regex(#"(([a-zA-Z])+(?:[\s\:\,]+))").Matches("...");
or this regex
"\b([a-zA-Z])+\b"
Iterate over the MatchCollection.
Related
I have many filenames such as:
libgcc1-5.2.0-r0.70413e92.rbt.xar
python3-sqlite3-3.4.3-r1.0.f25d9e76.rbt.xar
u-boot-signed-pad.bin-v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57.rbt.xar
I need to reliably extract the name, version and "rbt" or "norbt" from this. What is the best way? I am trying regex, something like:
(?<fileName>.*?)-(?<version>.+).(rbt|norbt).xar
Issue is the file name and version both can have multiple semi colons. So I am not sure if there is an answer by I have two questions:
What is the best strategy to extract values such as these?
How would I be able to figure out which version is greater?
Expected output is:
libgcc1, 5.2.0-r0.70413e92, rbt
python3-sqlite3, 3.4.3-r1.0.f25d9e76, rbt
u-boot-signed-pad.bin, v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57, rbt
This will give you what you want without using Regex:
var fileNames = new List<string>(){
"libgcc1-5.2.0-r0.70413e92.rbt.xar",
"python3-sqlite3-3.4.3-r1.0.f25d9e76.rbt.xar",
"u-boot-signed-pad.bin-v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57.rbt.xar"
};
foreach(var file in fileNames){
var spl = file.Split('-');
string name = string.Join("-",spl.Take(spl.Length-2));
string versionRbt = string.Join("-",spl.Skip(spl.Length-2));
string rbtNorbt = versionRbt.IndexOf("norbt") > 0 ? "norbt" : "rbt";
string version = versionRbt.Replace($".{rbtNorbt}.xar","");
Console.WriteLine($"name={name};version={version};rbt={rbtNorbt}");
}
Output:
name=libgcc1;version=5.2.0-r0.70413e92;rbt=rbt
name=python3-sqlite3;version=3.4.3-r1.0.f25d9e76;rbt=rbt
name=u-boot-signed-pad.bin;version=v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57;rbt=rbt
Edit:
Or using Regex:
var m = Regex.Match(file,#"^(?<fileName>.*)-(?<version>.+-.+)\.(rbt|norbt)\.xar$");
string name = m.Groups["fileName"].Value;
string version = m.Groups["version"].Value;
string rbtNorbt = m.Groups[1].Value;
The output will be the same. Both approaches assum that "version" has one -.
Tested following code and work perfectly with Regex. I used option Right-To-Left
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication107
{
class Program
{
static void Main(string[] args)
{
string[] inputs = {
"libgcc1-5.2.0-r0.70413e92.rbt.xar",
"python3-sqlite3-3.4.3-r1.0.f25d9e76.rbt.xar",
"u-boot-signed-pad.bin-v2015.10+gitAUTOINC+1b6aee73e6-r0.02df1c57.rbt.xar"
};
string pattern = #"(?'prefix'.+)-(?'middle'[^-][\w+\.]+-[\w+\.]+)\.(?'extension'[^\.]+).\.xar";
foreach (string input in inputs)
{
Match match = Regex.Match(input, pattern, RegexOptions.RightToLeft);
Console.WriteLine("prefix : '{0}', middle : '{1}', extension : '{2}'",
match.Groups["prefix"].Value,
match.Groups["middle"].Value,
match.Groups["extension"].Value
);
}
Console.ReadLine();
}
}
}
I'm new to C#, and it looks I need to use Regex with Dictionary<string, Action>
The below working example with me, as testing of understanding the Regex in C#:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string sPattern = "word1|word2";
string input = "word2";
Match result = Regex.Match(input, sPattern);
Console.WriteLine(result);
}
}
I tried to include it in Dictionary as below, but failed:
var functions = new Dictionary<Match, Action>();
functions.Add(Regex.Match(string, sPattern), CountParameters);
Action action;
if (functions.TryGetValue("word1|word2", out action)) {action.Invoke(); }
It gave me invalid expression string at Regex.Match(string, sPattern) and cannot convert string to Match at .TryGetValue("word1|word2")
UPDATE
I restructured my code like below, so I've no compiling error, but nothing is printed out as a result:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string sPattern1 = "word1|word2";
string sPattern2 = "word3|word4";
string input = "word2";
var functions = new Dictionary<string, Action>();
functions.Add("word1", CountParameters);
functions.Add("word3", SomeOtherMethodName);
Action action;
if (functions.TryGetValue((Regex.Match(input, sPattern1)).ToString(), out action))
{
action.Invoke();
}
else
{
// No function with that name
}
}
public static void CountParameters()
{
Console.WriteLine("Fn 1");
}
public static void SomeOtherMethodName()
{
Console.WriteLine("Fn 2");
}
}
The above is working if string input = "word1"; but not working if string input = "word2"; while the RegEx should consider both word1 and word2 as the same based on the string sPattern = "word1|word2";
UPDATE 2
In case it was not clear enough, the output of the above should be:
Executing CountParameters in case the input is word1 or word2, as the RegEx should consider them the same considering the | used in the pattern above.
Executing SomeOtherMethodName in case the input is word3 or word4, as the RegEx should consider them the same considering the | used in the pattern above.
and so on, in case I added more RegEx expression using the OR which is |
I think you want something like this:
var input = "word2";
var functions = new Dictionary<Regex, Action>
{
{new Regex("word1|word2"), CountParameters}
};
functions.FirstOrDefault(f => f.Key.IsMatch(input)).Value?.Invoke();
I am relatively new to C# programming and I apologize if this is a simple matter, but I need help with something.
I need a function which will 'extract' regular AND decimal numbers from a string and place them in an array. I'm familiar with
string[] extractData = Regex.Split(someInput, #"\D+")
but that only takes out integers. If I have a string "19 something 58" it will take 19 and 58 and store them into two different array fields. However if I had "19.58 something" it will again take them as two separate numbers, while I want to register it as one decimal number.
Is there a way to make it 'read' such numbers as one decimal number, using Regex or some other method?
Thanks in advance.
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication9
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
string[] inputs = {
"9 something 58" ,
"19.58 something"
};
foreach (string input in inputs)
{
MatchCollection matches = Regex.Matches(input, #"(?'number'\d*\.\d*)|(?'number'\d+[^\.])");
foreach (Match match in matches)
{
Console.WriteLine("Number : {0}", match.Groups["number"].Value);
}
}
Console.ReadLine();
}
}
}
Try this
Regex.Replace(someInput, "[^-?\d+\.]", ""))
So i have a txt file with names which i provided at the top of the code. The problem is i want to split those lines into seperate lines if they contain a ' ' (space)
John
James Peter
Mary Bob
Thomas
Michael
i want it to be like this:
John
James
Peter
Mary
Bob
Thomas
Michael
.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
#region test2
StreamReader Fixed = new StreamReader("SSfile.txt");
List<string> FixedList = new List<string>();
List<string> FixedList2 = new List<string>();
FixedList2.ToArray();
string ReadFixed = Fixed.ReadLine();
while (ReadFixed != null)
{
FixedList.Add(ReadFixed);
ReadFixed = Fixed.ReadLine();
}
Fixed.Close();
for (int i = 0; i < FixedList.Count(); i++)
{
FixedList[i].Split(' ');
}
Console.WriteLine(FixedList.ToString());
Console.ReadKey();
#endregion
Console.ReadKey();
}
}
}
So what should i do??
Use the power of LINQ:
String.Split will split a string into an array of substrings based on the given delimiter.
SelectMany will "flatten" a collection. That means you give a method that transforms one item into many items (for example one string into an array of string) and SelectMany will form a single enumerable from it.
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
var names = File.ReadLines("SSfile.txt")
.SelectMany(line => line.Split(' '));
foreach(var name in names)
{
Console.WriteLine(name);
}
}
}
}
Split each line, example:
string.Join("\n","James Peter".Split(' '));
i am reading a book named "Visual C# 2012 Programming" and i came up with following code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch05StringManupulationEx
{
class Program
{
static void Main(string[] args)
{
string myString = "String with small s";
char[] myChar = myString.ToCharArray();
foreach (char whatever in myString)
{
Console.WriteLine("{0}", whatever);
}
Console.Write("\nyou have entered {0} characters in String ",myString.Length);
Console.ReadKey();
}
}
}
i don't know what the aurthor is doing on line :
char[] myChar = myString.ToCharArray();
because he is not using the variable named myChar anywhere in the code and even though i commented the line and compiled the program the output is same, can any one explain what is the purpose of this line in this code ?
Probably they forgot to remove that line or show what does it do, It's an array of character, A string is full of characters, each letter of a string is a character, you can access any of these arrays by using a zero based numbers, for example:
string a = "Hello";
// Prints e
Console.WriteLine(a[2]);
You can change this line to myChar to understand, It's same to an array of string, Which means a string is an array of chars, here's the example:
foreach (char whatever in myChar)
{
Console.WriteLine("{0}", whatever);
}