I have the following all number data:
4245 4 0 0242 4424.09 0 422404 5955 0
2234234.234 224 0
2423 234 0
I need to process each line individually. I need to remove all the single 0's and output as follows with commas:
4245,4, 0242,4424.09, 422404,5955
2234234.234,224
2423,234
I got the part of removing the single digits working:
var result = Regex.Replace(inData, #"\b\s0\b", string.Empty);
But cannot figure out how to add the commas in between each number. Any help would be appreciated. Thanks.
You can achieve what you want with one Regex.Replace operation, but with a custom match evaluator:
var input = "4245 4 0 242 4424.09 0 422404 5955 0";
var results = Regex.Replace(input, #"(?:\s+|^)0(\s+)|(\s+0)$|\s+", m =>
m.Groups[1].Success ? ", " :
m.Groups[2].Success ? "" : ",");
The point is to match those parts we need and capture into groups, so that they can be further analyzed and an appropriate action could be taken.
Pattern details:
(?:\s+|^)0(\s+) - match 0 that is either at the start or with whitespaces before it and that is followed with 1 or more whitespaces (the whitespaces after 0 are stored in Group 1)
| - or
(\s+0)$ - Group 2 capturing one or more whitespaces, then a 0 at the end ($) of the string
| - or
\s+ - (3rd option) 1 or more whitespaces in all other contexts.
And just in case one likes a more readable version, here is an alternative where the final 0 is removed with string methods, and then 1 regex is used to replace all spaces inside digits with a comma, but before we replace all 0 s with a mere String.Replace.
var inp = "4245 4 0 0242 4424.09 0 422404 5955 0";
inp = inp.EndsWith(" 0") ? inp.Substring(0, inp.Length - 2) : inp;
var output = Regex.Replace(inp.Replace(" 0 ", ", "), #"(\d) (\d)", "$1,$2");
I understand that you want to
Replace spaces with commas ("xy z" => "xy,z")
Replace single zeros with spaces ("xy 0 z" => "xy, z")
Then I would recommend two string replacements:
inData.replace(" ", ",");,
inData.replace(",0", " ");
Using this will replace any whitespace character with a comma.
var result = Regex.Replace(inData, #"\s+", ",");
\s+ matches any whitespace character.
Then run your other regex to remove the single digit 0's
You could just do a string.replace(" ", ","), right? (if I am understanding your question correctly)
Or you could even do a string.split(" ") into an array, then string.join(','). Although this is probably less efficient.
Related
I need a regex for an input that contains positive and negative numbers and sometimes a string between " and ". I'm not sure if this can be done in only one pattern. Here's some test cases for the pattern:
*PATH "C:\Users\User\Desktop\Media\SoundBanks\Ambient\WAV_Data\AD_SMP_SFX_WIND0.wav"
*NODECOLOR 0 255 140
*FILEREF -7
*FREQUENCY 22050
The idea would be to use a pattern that returns:
C:\Users\User\Desktop\Media\SoundBanks\Ambient\WAV_Data\AD_SMP_SFX_WIND0.wav
0 255 140
-7
22050
The content always goes after the character *. I've split this in two patterns because I don't know how to do it all in one, but doesn't work:
MatchCollection NumberMtaches = Regex.Matches(FileLine, #"(?<=[*])-?[0-9]+");
MatchCollection FilePathMatches = Regex.Matches(FileLine, #"/,([^,]*)(?=,)/g");
You may read the file into a string and run the following regex:
var matches = Regex.Matches(filecontents, #"(?m)^\*\w+[\s-[\r\n]]*""?(.*?)""?\r?$")
.Cast<Match>()
.Select(x => x.Groups[1].Value)
.ToList();
See the .NET regex demo.
Details:
(?m) - RegexOptions.Multiline option on
^ - start of a line
\* - a * char
\w+ - one or more word chars
[\s-[\r\n]]* - zero or more whitespaces other than CR and LF
"? - an optional " char
(.*?) - Group 1: any zero or more chars other than an LF char, as few as possible
"? - an optional " char
\r? - an optional CR
$ - end of a line/string.
For example, if the entered input is:
1 2 3 |4 5 6 | 7 8
we should manipulate it to
1 2 3|4 5 6|7 8
Another example:
7 | 4 5|1 0| 2 5 |3
we should manipulate it to
7|4 5|1 0|2 5|3
This is my idea because I want to exchange some of the subarrays (7; 4 5; 1 0; 2 5; 3).
I'm not sure that this code is working and it can be the base of I want to do but I must upload it for you to see my work.
static void Main(string[] args)
{
List<string> arrays = Console.ReadLine()
.Split(' ', StringSplitOptions.RemoveEmptyEntries)
.ToList();
foreach (var element in arrays)
{
Console.WriteLine("element: " + element);
}
}
You need to split your input by "|" first and then by space. After this, you can reassemble your input with string.Join. Try this code:
var input = "1 2 3 |4 5 6 | 7 8";
var result = string.Join("|", input.Split('|')
.Select(part => string.Join(" ",
part.Trim().Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries))));
// now result is "1 2 3|4 5 6|7 8"
This could do this with a simple regular expression:
var result = Regex.Replace(input, #"\s?\|\s?", "|");
This will match any (optional) white space character, followed by a | character, followed by an (optional) white space character and replace it with a single | character.
Alternatively, if you need to potentially strip out multiple spaces around the |, replace the zero-or-one quantifiers (?) with zero-or-more quantifiers (*):
var result = Regex.Replace(input, #"\s*\|\s*", "|");
To also deal with multiple spaces between numbers (not just around | characters), I'd recommend something like this:
var result = Regex.Replace(input, #"\s*([\s|])\s*", "$1")
This will match any occurrence of zero or more white space characters, followed by either a white space character or a | character (captured in group 1), followed by zero or more white space characters and replace it with whatever was captured in group 1.
Here i have following strings ,
"#,##0.00"
"\"$\"#,##0.0000"
I need to split using regular expressions.
My Expected output is
"#,##0.00" => 2 (decimal)
"\"$\"#,##0.0000" => $4(4 decimal with $)
How to convert can u please suggest any way.
Thanks
You may use the following regex:
^(?:"([^"]+)")?.*?(0+)$
The pattern matches:
^ - start of string
(?:"([^"]+)")? - 1 or 0 sequences of:
" - a double quote
([^"]+) - Group 1 capturing 1 or more chars other than "
"
.*? - any characters other than newline, 0 or more repetitions
(0+) - Group 2 capturing 1 or more zeros
$ - end of string
And here is a C# demo:
var pat = #"^(?:""([^""]+)"")?.*?(0+)$";
var match = Regex.Match("#.##0.00", pat);
if (match.Success) {
Console.WriteLine(match.Groups[1].Value + match.Groups[2].Length.ToString());
} // => 2
// With "\"$\"#,##0.0000" input: $4
See the IDEONE demo
I have this particular string:
Administrationsomkostninger I -2.889 - r0.l l0
I would like to replace these characters:r,l and i with 1.
I use this expression:
([(t|r|l|i|)])
That gives me this string:
Adm1n1s11a11onsomkos1n1nge1 1 -2.889 - 10.1 10
Now i want to replace the all digits that contains a digit followed + a whitespace
so in this case only - 10.1 10 gets converted to -10.110
Try this
string input = "Administrationsomkostninger I -2.889 - r0.l l0";
string pattern = #"(?'spaces'\s){2,}";
string output = Regex.Replace(input, pattern, " ");
I currently have a regex that pulls up a 16 digit number from a file e.g.:
Regex:
Regex.Match(l, #"\d{16}")
This would work well for a number as follows:
1234567891234567
Although how could I also include numbers in the regex such as:
1234 5678 9123 4567
and
1234-5678-9123-4567
If all groups are always 4 digit long:
\b\d{4}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}\b
to be sure the delimiter is the same between groups:
\b\d{4}(| |-)\d{4}\1\d{4}\1\d{4}\b
If it's always all together or groups of fours, then one way to do this with a single regex is something like:
Regex.Match(l, #"\d{16}|\d{4}[- ]\d{4}[- ]\d{4}[- ]\d{4}")
You could try something like:
^([0-9]{4}[\s-]?){3}([0-9]{4})$
That should do the trick.
Please note:
This also allows
1234-5678 9123 4567
It's not strict on only dashes or only spaces.
Another option is to just use the regex you currently have, and strip all offending characters out of the string before you run the regex:
var input = fileValue.Replace("-",string.Empty).Replace(" ",string.Empty);
Regex.Match(input, #"\d{16}");
Here is a pattern which will get all the numbers and strip out the dashes or spaces. Note it also checks to validate that there is only 16 numbers. The ignore option is so the pattern is commented, it doesn't affect the match processing.
string value = "1234-5678-9123-4567";
string pattern = #"
^ # Beginning of line
( # Place into capture groups for 1 match
(?<Number>\d{4}) # Place into named group capture
(?:[\s-]?) # Allow for a space or dash optional
){4} # Get 4 groups
(?!\d) # 17th number, do not match! abort
$ # End constraint to keep int in 16 digits
";
var result = Regex.Match(value, pattern, RegexOptions.IgnorePatternWhitespace)
.Groups["Number"].Captures
.OfType<Capture>()
.Aggregate (string.Empty, (seed, current) => seed + current);
Console.WriteLine ( result ); // 1234567891234567
// Shows False due to 17 numbers!
Console.WriteLine ( Regex.IsMatch("1234-5678-9123-45678", pattern, RegexOptions.IgnorePatternWhitespace));