I'm writing a c# code that divide a string into two different groups. a string is pipe-delimited as example below:
there could be an empty space between two pipes.
number of pipes to "5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w" are fixed; In this case, there are 4 pipes.
string value = "122312121|test value||test value 2|5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w|123456789|123456789";
const string sPattern = #"What should it be here?????";
var regex = new Regex(sPattern);
var match = regex.Match(value);
if (match.Success)
{
var begin = match.Groups["begin"].Value;
var middle = match.Groups["middle"].Value;
var end = match.Groups["end"].Value;
}
I am trying to get the output of the code to return as following:
begin = "122312121|test value||test value 2|"
middle = "5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w"
end = "|123456789|123456789"
However, I'm so new to regular expression, and I have tried to write a regular expression for variable sPattern, but could not produce the right regular expression for it. Could any please help? Thanks.
you should use String.Split
string [] sarray = value.Split('|')
What that will do is give you the array
{"122312121", "test value", "" , "test value" , "2", "5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w", "123456789", "123456789"}
and 5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w will be in sarray[5]
If you're looking for a regular expression to match this and want to use a regular expression rather than .Split, you could try this:
"^((.*?[|]){4})(.*?)([|].*)*$"
or more explicitly:
"^(?<begin>(.*?[|]){4})(?<middle>.*?)(?<end>[|].*)*$"
This is based on the fact that you said the number of pipes before your long string is fixed (at four).
Your code would then read as follows:
string value = "122312121|test value||test value 2|5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w|123456789|123456789";
const string sPattern = #"^((.*?[|]){4})(.*?)([|].*)*$";
var regex = new Regex(sPattern);
var match = regex.Match(value);
if (match.Success)
{
var begin = match.Groups[1].Value;
var middle = match.Groups[3].Value;
var end = match.Groups[4].Value;
}
The trick may be to escape the pipe character:
const string sPattern = #"(?<begin>[^|]*\|[^|]*\|[^|]*\|[^|]*\|)" +
"(?<middle>[^|]*)" +
"(?<end>\|.*)";
You could use String.Split and some Linq to do what you need
Rough example:
string value = "122312121|test value||test value 2|5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w|123456789|123456789";
string[] split = value.Split('|');
string begin = string.Join("|", split.Take(4));
string middle = split.Skip(4).Take(1).FirstOrDefault();
string end = "|" + string.Join("|", split.Skip(5).Take(2));
Returns
begin = "122312121|test value||test value 2|"
middle = "5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w"
end = "|123456789|123456789"
Here's another one:
^(?<begin>(.*?\|){4})(?<middle>.*?(?=\|))(?<end>.*)
Related
if I have:
string newMangerName = "RandomName";
string manager = "CN=Kylie Seany,OU=Test,OU=Users"
So the CN name value will be different for every manager,
I want to replace the CN name value with newManger
e.g
string newManager = manager.replace("afterCN=", replace(newManagerName);
You could use RegEx:
var src = "CN=Kylie Seany,OU=Test,OU=Users";
var res = Regex.Replace(src, "(?<=CN\\=)(.*?)(?=,|$)", "test");
Console.WriteLine(res);
(?<=CN\=) is a lookbehind requiring that the previous characters were CN=
(?=,|$) is a lookahead requiring that the next character is , or the end of the string.
(.*?) is a non-greedy match-all.
It's simple, if format is fixed you can easily use Indexof and Substring string functions:
string newManagerName = "Some Random Name";
string manager = "CN=Kylie Seany,OU=Test,OU=Users";
int index = manager.IndexOf(",OU=");
string newManager = "CN=" + newManagerName + manager.Substring(index);
Console.WriteLine(newManager);
I need to replace multiple file names in a folder. Here is one of the files:
Abc.CDE.EFG
I need to replace the first part of the string before the dot ("ABC") and replace it with: "zef".
Any ideas? I found this but it takes out the dot and not sure how to add the "zef".
var input = _FileInfo.ToString();
var output = input.Substring(input.IndexOf(".").Trim())
Since the question is tagged with regex, you can use a regular expression like so:
var input = "abc.def.efg";
var pattern = "^[^\\.]+";
var replacement = "zef";
var rgx = new Regex(pattern);
var output = rgx.Replace(input, replacement);
Source: https://msdn.microsoft.com/en-us/library/xwewhkd1(v=vs.110).aspx
You are almost there, try:
string myString = "Abc.CDE.EFG";
//This splits your string into an array with 3 items
//"Abc", "CDE" and "EFG"
var stringArray = myString.Split('.');
//Now modify the first item by changing it to "zef"
stringArray[0] = "zef";
//Then we rebuild the string by joining the array together
//delimiting each group by a period
string newString = string.Join(".", stringArray);
With this solution you can independently access any of the "blocks" just by referencing the array by index.
Fiddle here
Try this:
var input = _FileInfo.ToString();
var output = "zef" + input.Substring(input.IndexOf("."));
If you know the length of the first string , you can replace mentioning number of characters starting from position until the length you want to replace else.
string s = "Abc.CDE.EFG";
string [] n = s.Split('.');
n[0] = "ZEF";
string p = string.Join(".",n);
Console.WriteLine(p);
}
Lets say i have string like this Test%Test and i have stored strings like this:
Test123Test
TestTTTTest
Test153jhdsTest
123Test
TEST123
So what i want is when i type in textbox Test it would filter me everything with Test in itselft and that will get me all strings which is easy, but i want to type in Test%Test and it needs to filter me everything that has Test[anything]Test in itself (so result would be first, second and third string). How can i do it?
a simple solution using a regex is:
string[] values = new string[] { "Test123Test",
"TestTTTTest",
"Test153jhdsTest",
"123Test",
"TEST123" };
string searchQuery = "Test%Test";
string regex = Regex.Escape(searchQuery).Replace("%", ".*?");
string[] filteredValues = values.Where(str => Regex.IsMatch(str, regex)).ToArray();
Or for a single match:
string value = "Test123Test";
string searchQuery = "Test%Test";
string regex = Regex.Escape(searchQuery).Replace("%", ".*?");
if ( Regex.IsMatch(value, regex) )
{
// do something with the match...
}
We replace % with a regular expression (. = any character, * = zero or more times, ? = lazy quantifier). You can learn more about regular expressions here
534-W1A-R1 this is my file name and I want to split it so it prints like
Code=534 Phase=1 Zone=A
in my Autocad file.
The below split code should work:
string str = #"534-W1A-R1";
var split = str.Split('-');
string code = split.First();
string phase = new string(split.ElementAt(1).Skip(1).Take(1).ToArray());
string zone = new string(split.ElementAt(1).Skip(2).Take(1).ToArray());
string result = String.Format("Code={0} Phase={1} Zone={2}", code, phase, zone);
Console.WriteLine(result);
Output:
Code=534 Phase=1 Zone=A
Use the Substring() method.
string input = "534-W1A-R1";
string sub = input.Substring(0, 3);
string sub2 = input.Substring(5, 1);
string sub3 = input.Substring(6, 1);
Console.WriteLine("Code={0} Phase={1} Zone={2}", sub, sub2, sub3);
Output:
Code=534 Phase=1 Zone=A
You have different ways to do it. if you are sure about the format of the text you can just use this:
var str= "534-W1A-R1";
var parts=str.Split('-');
var code= parts[0];
var secondPart= parts[1];
var phase=secondPart.Substring(1,secondPart.Length-2);
var zone=secondPart[secondPart.Length-1];
You can also use Regex if it is more complicated.
Using Regex
Edit: added some comments (pattern description)
var pattern = #"^(\d+)-[A-Z](\d+)([A-Z])-";
/* pattern description:
^(\d+) group 1: one or more digits at the begining
- one hyphen (literal)
[A-Z] one alphabetic character
(\d+) group 2: one or more digits
([A-Z]) group 3: one alphabetic character
- one hyphen (literal)
*/
var input = "534-W1A-R1";
var groups = Regex.Match(input, pattern, RegexOptions.IgnoreCase).Groups;
var code = groups[1].Value;
var phase = groups[2].Value;
var zone = groups[3].Value;
i have some string like the ones below:
hu212 text = 1
reference = 1
racial construction = 1
2007 = 1
20th century history = 2
and i want to take only the integer AFTER the '='.. how can i do that?
i am trying this:
Regex exp = new Regex(#"[a-zA-Z]*[0-9]*[=][0-9]+",RegexOptions.IgnoreCase);
try
{
MatchCollection MatchList = exp.Matches(line);
Match FirstMatch = MatchList[0];
Console.WriteLine(FirstMatch.Value);
}catch(ArgumentOutOfRangeException ex)
{
System.Console.WriteLine("ERROR");
}
but it is not working...
i tryed some others but i get results like "20th" or "hu212"...
What exaclty Matches does? gives me the rest of the string that doesn match with the reg?
Instead of Regex you could also do:
int match = int.Parse(line.SubString(line.IndexOf('=')).Trim());
You need to allow whitespace (\s) between the = and the digits:
Regex pattern = new Regex(#"=\s*([0-9]+)$");
Here's a more complete example:
Regex pattern = new Regex(#"=\s*([0-9]+)$");
Match match = pattern.Match(input);
if (match.Success)
{
int value = int.Parse(match.Groups[1].Value);
// Use the value
}
See it working online: ideone
what about
string str = "hu212 text = 1"
string strSplit = str.split("=")[1].trim();
String StringToParse = "hu212 text = 1";
String[] splitString = String.Split(StringToParse);
Int32 outNum;
Int32.TryParse ( splitString[splitString.Length-1], out outNum );
Regex pattern = new Regex(#"=\s?(\d)");
This allow to have with or without space. The number is in group 1.
hu212 text =1
reference = 1