What is the proper way to split a string by regex [duplicate] - c#

This question already has answers here:
How to keep the delimiters of Regex.Split?
(6 answers)
Closed 6 years ago.
I have a text string with a custom macro:
"Some text {MACRO(parameter1)} more text {MACRO(parameter2)}"
In order to process the macro I want to split the string like that:
expected result
"Some text "
"{MACRO(parameter1)}"
" more text "
"{MACRO(parameter2)}"
I've tried to split the string using Regex.Split()
public static string[] Split(string input)
{
var regex = new Regex(#"{MACRO\((.*)\)}");
var lines = regex.Split(input)
return lines;
}
However Regex.Split() deletes the match itself and gives me the following:
actual result
"Some text "
" more text "
I know I could parse the string doing iterations of .Match() and .Substring()
But is there an easy way get the result along with the matches?

Try this
string input = "Some text {MACRO(parameter1)} more text {MACRO(parameter2)}";
string pattern = #"(?'text'[^\{]+)\{(?'macro'[^\}]+)\}";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
Console.WriteLine("text : '{0}'; macro : '{1}'", match.Groups["text"].Value.Trim(), match.Groups["macro"].Value.Trim());
}
Console.ReadLine();

Related

How to strip only the "\n" character that is inside double quotes using regex [duplicate]

This question already has answers here:
Replace all occurrences of the Tab character within double quotes
(2 answers)
Regex replace enters between quotes
(2 answers)
Closed 3 years ago.
The text patten would be like this
\n "this is some text \n this should not be on the new line"
I want to detect only the \n that is inside the double quotes and have it stripped out.
So the end result would be
\n "this is some text this should not be on the new line"
Try something like this fiddle
using System;
using System.Text.RegularExpressions;
public class Simple
{
static string StripLinefeeds(Match m)
{
string x = m.ToString(); // Get the matched string.
return x.Replace("\n", "");
}
public static void Main()
{
Regex re = new Regex(#""".+?""", RegexOptions.Singleline);
MatchEvaluator me = new MatchEvaluator(StripLinefeeds);
string text = "\n\"this is some text \n this should not be on the new line\"";
Console.WriteLine(text);
text = re.Replace(text, me);
Console.WriteLine(text);
text = "\n\"this is some \n text \n\n\n this should not be \n\n on the new line\"";
Console.WriteLine(text);
text = re.Replace(text, me);
Console.WriteLine(text);
}
}
This pattern might work for you.
/(?<=(\"(.|\n)*))(\n(?=((.|\n)*\")))/g
This is a "look-behind" (?<=(\"(.|\n)*))(...) and a "look-ahead" \n(?=((.|\n)*\")) paired together. It will only be reliable with one pair of quotes at most though, so if your needs change, you will have to adjust for that.
I think this would be the simplest way:
Match ("\w.[^\\]+)\\n ?
And replace with \1
https://regex101.com/r/8UXMzZ/3

c# regex won't match newline [duplicate]

This question already has answers here:
C# Regex Match between with or without new lines
(2 answers)
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 4 years ago.
I am trying to use a regex in c# to parse a multi line file ( Currently just in a string ) And this part seems to be where the problem is (.*?\n) it will split the .v/L1 and .v/L2 however when I put a nl between it fails the input file will look something like this.
MSG
.v/1L
.v/2L
.some other data
.and so on
ENDMSG
And this is part of the c# code
string nl = new string(new char[] { '\u000A' });
string pattern = #"(?<group>((?<type>MSG\n)(.*?\n)(?<end>ENDMSG\n)))";
string input = #" MSG" + nl + ".v/1L.v/2L" + nl + "ENDMSG" + nl;
// The Line below doesn't work
//string input = #" MSG" +nl+ ".v/1L" +nl+ ".v/2L" +nl+ "ENDMSG" + nl;
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
And this is the output of the one that works:
Starting RegEx !
RegEx : (?<group>((?<type>MSG\n)(.*?\n)(?<end>ENDMSG\n)))
'MSG
.v/1L.v/2L << Should be split into 2 when adding a \n between
ENDMSG
' found at index 1.
Executing finally block.
If you specify RegexOptions.Multiline then you can use ^ and $ to match the start and end of a line, respectively.
If you don't wish to use this option, remember that a new line may be any one of the following: \n, \r, \r\n, so instead of looking only for \n, you should perhaps use something like: [\n\r]+, or more exactly: (\n|\r|\r\n).

Split a string after reading square brackets in c# [duplicate]

This question already has answers here:
splitting a string based on multiple char delimiters
(7 answers)
Closed 5 years ago.
string test = "Account.Parameters[\"AccountNumber\"].Caption";
string new = test.Trim("[");
I want output "AccoutNumber".
I have tried the below code but not getting the desired result:
string[] test = "Transaction.Parameters[\"ExpOtherX\"].Caption".Split('[');
string newvalue = test[1];
Just use Split with two delimiters:
string[] test = "Transaction.Parameters[\"ExpOtherX\"].Caption".Split('[', ']');
string newvalue = test[1];
You can also use Regex:
string test = "Account.Parameters[\"AccountNumber\"].Caption";
var match = System.Text.RegularExpressions.Regex.Match(test, ".*?\\.Parameters\\[\"(.*?)\"]");
if (match.Success)
{
Console.WriteLine(match.Groups[1].Value);
}
.*? is a non greedy wildcart capture, so it will match your string until it reaches the next part (in our case, it will stop at .Parameters[", match the string, and then at "])
It will match .Parameters["..."]., and extract the "..." part.
you can do a Split to that string...
string test = "Account.Parameters[\"AccountNumber\"].Caption";
string output = test.Split('[', ']')[1];
Console.WriteLine(output);

Regex replace semicolon between quotation marks C# [duplicate]

This question already has answers here:
How to remove all commas that are inside quotes (") with C# and regex
(3 answers)
Closed 3 years ago.
I want to replace all semicolons that are enclosed in quotation marks with a space. How can I do this in C#?
For example:
this string:
this is an example; "this is other ; example"
Would return:
this is an example; "this is other example"
I await your help!
Edited: This will work.
string yourString = "hello there; \"this should ; be replaced \"";
string fixedString = Regex.Replace(yourString, "(\"[^\",]+);([^\"]+\")", delegate (Match match)
{
string v = match.ToString();
return v.Replace(";", " ");
});
Try following :
string input = "this is an example; \"this is other ; example\"";
string pattern = "\"(?'prefix'[^;]+);(?'suffix'[^\"]+)\"";
string output = Regex.Replace(input,pattern,"\"${prefix} ${suffix}\"");

How to extract number values from string mix with numbers and characters in C#? [duplicate]

This question already has answers here:
How to get number from a string
(5 answers)
Closed 7 years ago.
I am quite new to regular expression. My requirement is to extract number from string that includes mix of numbers and characters. I have tried below codes but I can only get the first number from string.
String serialNumber= "000745 TO 000748,00050-00052"
Match match = Regex.Match(serialNumber), #"(\d)+", RegexOptions.IgnoreCase);
if (match.Success)
{
int a = Convert.ToInt32(match); // This part not sure how to do
}
Expected result is:
000745
000748
00050
00052
string strRegex = #"\d+";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = #"000745 TO 000748,00050-00052";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}
You need to loop through your matches to get all the matches.

Categories