Simple Regex Befuddlement - c#

I have some strings of the form
string strA = "Cmd:param1:'C:\\SomePath\SomeFileName.ext':param2";
string strB = "Cmd:'C:\\SomePath\SomeFileName.ext':param2:param3";
I want to split this string on ':' so I can extract the N parameters. Some parameters can contain file paths [as explicitly] shown and I don't want to split on the ':'s that are within the parentheses. I can do this with a regex but I am confused as to how to get the regex to split only if there is no "'" on both sides of the colon.
I have attempted
string regex = #"(?<!'):(?!')";
string regex = #"(?<!'(?=')):";
that is continue matching only if no "'" on the left and no "'" on the right (negative look behind/ahead), but this is still splitting on the colon contained in 'C:\SomePath\SomeFileName.ext'.
How can I amend this regex to do as I require?
Thanks for your time.
Note: I have found that the following regex works. However, I would like to know if there is a better way of doing this?
string regex = #"(?<!.*'.*):|:(?!.*'.*)";

Consider this approach:
var guid = Guid.NewGuid().ToString();
var r = Regex.Replace(strA, #"'.*'", m =>
{
return m.Value.Replace(":", guid);
})
.Split(':')
.Select(s => s.Replace(guid, ":"))
.ToList();

Rather than try to construct a lookbehind regex to split on, you could construct a regex to match the fields themselves and take the set of matches of that regex. EG a field is either a quoted sequence of non-quotes (ie can include :), or it can't include the separator:
string regex = "'[^']*'|[^':]*";
var result = Regex.Matches(strA, regex);

You want to split on (?<!\b[a-z]):(?!\\) (use RegexOptions.IgnoreCase).

Not as pretty but you could replace :\ with safe characters and then return them back to :\ after the split.
string[] param = strA.Replace(#":\", "|||").Split(':').Select(x => x.Replace("|||", #":\")).ToArray();

Related

c# regex replace everything including a word base64, with nothing and keeping rest of the string

I am wanting to take a string and find base64, and get rid of that and everything prior to that
example
"asdfjljlkjaldf_base64,234u0909230948098234082304802384023094"
Notice "base64," ... I want ONLY everything after "base64,"
Desired: "234u0909230948098234082304802384023094"
I was looking at this code
"string test = "hello, base64, matching";
string regexStrTest;
regexStrTest = #"test\s\w+";
MatchCollection m1 = Regex.Matches(base64,, regexStrTest);
//gets the second matched value
string value = m1[1].Value;
but that is not quite what I want..
Why regular expressions? IndexOf + Substring seems to be quite enough:
string source = "asdfjljlkjaldf_base64,234u0909230948098234082304802384023094";
string tag = "base64,";
string result = source.Substring(source.IndexOf(tag) + tag.Length);
You tried a regex that matches test, a whitespace, and 1+ word chars. The input string just did not match it.
You may use
var results = Regex.Matches(s, #"base64,(\w+)")
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToList();
See the regex demo.
The pattern matches base64, substring and then captures into Group 1 one or more word chars with (\w+) pattern. The captured value is stored inside match.Groups[1].Value, just what you get with .Select(m => m.Groups[1].Value).
Some of the other answers are good. Here is a very simple regex
string yourData = "asdfjljlkjaldf_base64,234u0909230948098234082304802384023094";
var newString = Regex.Replace(yourData, "^.*base64,", "");

Using regex to remove everything that is not in between '<#'something'#>' and replace it with commas

I have a string, for example
<#String1#> + <#String2#> , <#String3#> --<#String4#>
And I want to use regex/string manipulation to get the following result:
<#String1#>,<#String2#>,<#String3#>,<#String4#>
I don't really have any experience doing this, any tips?
There are multiple ways to do something like this, and it depends on exactly what you need. However, if you want to use a single regex operation to do it, and you only want to fix stuff that comes between the bracketed strings, then you could do this:
string input = "<#String1#> + <#String2#> , <#String3#> --<#String4#>";
string pattern = "(?<=>)[^<>]+(?=<)";
string replacement = ",";
string result = Regex.Replace(input, pattern, replacement);
The pattern uses [^<>]+ to match any non-pointy-bracket characters, but it combines it with a look-behind statement ((?<=>)) and a look-ahead statement (?=<) to make sure that it only matches text that occurs between a closing and another opening set of brackets.
If you need to remove text that comes before the first < or after the last >, or if you find the look-around statements confusing, you may want to consider simply matching the text that comes between the brackets and then loop through all the matches and build a new string yourself, rather than using the RegEx.Replace method. For instance:
string input = "sdfg<#String1#> + <#String2#> , <#String3#> --<#String4#>ag";
string pattern = #"<[^<>]+>";
List<String> values = new List<string>();
foreach (Match m in Regex.Matches(input, pattern))
values.Add(m.Value);
string result = String.Join(",", values);
Or, the same thing using LINQ:
string input = "sdfg<#String1#> + <#String2#> , <#String3#> --<#String4#>ag";
string pattern = #"<[^<>]+>";
string result = String.Join(",", Regex.Matches(input, pattern).Cast<Match>().Select(x => x.Value));
If you're just after string manipulation and don't necessarily need a regex, you could simply use the string.Replace method.
yourString = yourString.Replace("#> + <#", "#>,<#");

Splitting on “,” but not “/,”

Question: How do I write an expression to split a string on ',' but not '/,'? Later I'll want to replace '/,' with ', '.
Details...
Delimiter: ','
Skip Char: '/'
Example input: "Mister,Bill,is,made,of/,clay"
I want to split this input into an array: {"Mister", "Bill", "is", "made", "of, clay"}
I know how to do this with a char prev, cur; and some indexers, but that seems beta.
Java Regex has a split functionality, but I don't know how to replicate this behavior in C#.
Note: This isn't a duplicate question, this is the same question but for a different language.
I believe you're looking for a negative lookbehind:
var regex = new Regex("(?<!/),");
var result = regex.Split(str);
this will split str on all commas that are not preceded by a slash. If you want to keep the '/,' in the string then this will work for you.
Since you said that you wanted to split the string and later replace the '/,' with ', ', you'll want to do the above first then you can iterate over the result and replace the strings like so:
var replacedResult = result.Select(s => s.Replace("/,", ", ");
string s = "Mister,Bill,is,made,of/,clay";
var arr = s.Replace("/,"," ").Split(',');
result : {"Mister", "Bill", "is", "made", "of clay"}
Using Regex:
var result = Regex.Split("Mister,Bill,is,made,of/,clay", "(?<=[^/]),");
Just use a Replace to remove the commas from your string :
s.Replace("/,", "//").Split(',').Select(x => x.Replace("//", ","));
You can use this in c#
string regex = #"(?:[^\/]),";
var match = Regex.Split("Mister,Bill,is,made,of/,clay", regex, RegexOptions.IgnoreCase);
After that you can replace /, and continue your operation as you like

C# RegEx - get only first match in string

I've got an input string that looks like this:
level=<device[195].level>&name=<device[195].name>
I want to create a RegEx that will parse out each of the <device> tags, for example, I'd expect two items to be matched from my input string: <device[195].level> and <device[195].name>.
So far I've had some luck with this pattern and code, but it always finds both of the device tags as a single match:
var pattern = "<device\\[[0-9]*\\]\\.\\S*>";
Regex rgx = new Regex(pattern);
var matches = rgx.Matches(httpData);
The result is that matches will contain a single result with the value <device[195].level>&name=<device[195].name>
I'm guessing there must be a way to 'terminate' the pattern, but I'm not sure what it is.
Use non-greedy quantifiers:
<device\[\d+\]\.\S+?>
Also, use verbatim strings for escaping regexes, it makes them much more readable:
var pattern = #"<device\[\d+\]\.\S+?>";
As a side note, I guess in your case using \w instead of \S would be more in line with what you intended, but I left the \S because I can't know that.
depends how much of the structure of the angle blocks you need to match, but you can do
"\\<device.+?\\>"
I want to create a RegEx that will parse out each of the <device> tags
I'd expect two items to be matched from my input string:
1. <device[195].level>
2. <device[195].name>
This should work. Get the matched group from index 1
(<device[^>]*>)
Live demo
String literals for use in programs:
#"(<device[^>]*>)"
Change your repetition operator and use \w instead of \S
var pattern = #"<device\[[0-9]+\]\.\w+>";
String s = #"level=<device[195].level>&name=<device[195].name>";
foreach (Match m in Regex.Matches(s, #"<device\[[0-9]+\]\.\w+>"))
Console.WriteLine(m.Value);
Output
<device[195].level>
<device[195].name>
Use named match groups and create a linq entity projection. There will be two matches, thus separating the individual items:
string data = "level=<device[195].level>&name=<device[195].name>";
string pattern = #"
(?<variable>[^=]+) # get the variable name
(?:=<device\[) # static '=<device'
(?<index>[^\]]+) # device number index
(?:]\.) # static ].
(?<sub>[^>]+) # Get the sub command
(?:>&?) # Match but don't capture the > and possible &
";
// Ignore pattern whitespace is to document the pattern, does not affect processing.
var items = Regex.Matches(data, pattern, RegexOptions.IgnorePatternWhitespace)
.OfType<Match>()
.Select (mt => new
{
Variable = mt.Groups["variable"].Value,
Index = mt.Groups["index"].Value,
Sub = mt.Groups["sub"].Value
})
.ToList();
items.ForEach(itm => Console.WriteLine ("{0}:{1}:{2}", itm.Variable, itm.Index, itm.Sub));
/* Output
level:195:level
name:195:name
*/

Csharp: Find & Replace string match between specific characters || in a string using Regex

I have a string like:
string originalStringBefore = "http://www.abc.com?a=||ThisIsForRndNumber||&qq=hello&jj=||ThisIsForRndNumberAlso||";
I want every string which comes between || to be replaced with a random number.
Now, the random number generation is easy, but i can't find the way to write a Regular expression to search the string using a pattern and replace it.
I don't want to do it by string manipulation functions.
Expected Solution/ outcome:
string originalStringAfter = "http://www.abc.com?a=||254877787||&qq=hello&jj=||6594741454||";
string originalStringBefore = "http://www.abc.com?a=||ThisIsForRndNumber||&qq=hello&jj=||ThisIsForRndNumberAlso||";
Random r = new Random();
Regex rgx = new Regex(#"\|\|.*?\|\|");
Console.WriteLine(rgx.Replace(originalStringBefore, "||" + r.Next(int.MaxValue) + "||"));
It looks like you want this regex:
(?<=\|\|)\w+(?=\|\|)
which finds alphanumeric between || and leaves strings that contain non-alphanumeric characters (like &) alone.
Then, in C#:
public String ComputeReplacement(Match m) {
return RandomNumberString();
}
resultString = Regex.Replace(subjectString, #"(?<=\|\|)\w+(?=\|\|)", new MatchEvaluator(ComputeReplacement));
Please find the regular expression replace command pasted below
string result = Regex.Replace(originalStringBefore, #"\|\|.*?\|\|","||ReplaceCharactors||");
how ever to write reguler expression's go through this site hope this wil help you
Thanks.

Categories