Converting boundary points from a .kml using regex - c#

I have this boundary that I received from a kml, I was able to dig down the xml and grab just the boundary points. I need to convert the points from this :
-92.25968002689014,30.7180061776264,0 -92.25976564548085,30.71751889774971,0 -92.25992462712097,30.71670626485147,0 -92.26006418327708,30.71604891951008,0 -92.26018466460856,30.71558863525373,0 -92.26037301574165,30.71498469610939,0 -92.26054805030229,30.71444051930294,0 -92.26065861561004,30.71411636559884,0
To This:
POLYGON((-92.25968002689014 30.7180061776264, -92.25976564548085,30.71751889774971, -92.25992462712097 30.71670626485147, -92.26006418327708,30.71604891951008, -92.26018466460856 30.71558863525373, -92.26037301574165,30.71498469610939, -92.26054805030229 30.71444051930294, -92.26065861561004,30.71411636559884))
The regex pattern I am using is : ",[0-9.-]* *"
My plan was to use a regex replace to replace any commas followed by any number of digits, periods, or minus signs followed by one or more spaces with some character like a colon. Then replace all commas with spaces and then replae all colons with commas. But for some reason I can't get it to work. Any Advice would be greatly appreciated.

You can try this:
([-\d.]+),([-\d.]+),([-\d.]+)\s+([-\d.]+),([-\d.]+),([-\d.]+)\s*;
Sample c# code:
String polygon(String input)
{
string pattern = #"([-\d.]+),([-\d.]+),([-\d.]+)\s+([-\d.]+),([-\d.]+),([-\d.]+)\s*";
RegexOptions options = RegexOptions.Singleline | RegexOptions.Multiline;
String finalString = "POLYGON((";
int count = 0;
foreach (Match m in Regex.Matches(input, pattern, options))
{
if (count > 0)
finalString += ",";
finalString += m.Groups[1] + " " + m.Groups[2] + ", " + m.Groups[4] + "," + m.Groups[5];
count = 1;
}
finalString += "))";
return finalString;
}
output:
POLYGON((-92.25968002689014 30.7180061776264, -92.25976564548085,30.71751889774971,-92.25992462712097 30.71670626485147,
-92.26006418327708,30.71604891951008,-92.26018466460856 30.71558863525373, -92.26037301574165,30.71498469610939,-92.260
54805030229 30.71444051930294, -92.26065861561004,30.71411636559884))

Related

regex c# string

I would like to transform this claim that I get "[\" 75 \ ", \" 91 \ "]" to (75,91)
I used regex.split but I don't know how to do it.
if (Zone != null)
{
filtrer.Append(" and depalcement in (");
foreach (string i in Zone)
{
var diviser=Regex.Split(i, #"\");
filtrer.Append(diviser);
}
}
No Regex, just JSON. Try this:
var content = "[\"75\",\"91\"]";
var list = System.Text.Json.JsonSerializer.Deserialize<List<string>>(content);
var numbers = list.Select(int.Parse);
foreach(var number in numbers)
{
Console.WriteLine(number);
}
For completeness; to get from "[\" 75 \", \" 91 \"]" to (75,91) using regex the code could be something like this:
var s2 = "(" + Regex.Replace(input: s, pattern: "[^0-9,]", "") + ")"; // or #"[^\d,]"
Pattern:
[...] - character set aka character class aka 'any of'
^ - negate
0-9 or \d - digit
, comma
together it's any character that isn't a digit or a comma

how to split a text tile of names into 4 separate array indexes in c#

I am formatting text in a file and need to have each word in the file as a separate array index. here is an example of the text in the file:
Sanders, Bernie M Democrat
Boehner, John M Republican
Pelosi, Nancy F Democrat
Rubio, Mario M Republican
here is what I have tried, to separate the text.
char[] whitespace = new char[] { ' ', '\t'};
string[] separate = line.Split(whitespace);
There are several spaces before and after the "m" or "f", which makes it hard to separate, since they are regular spaces and not created with a tab. so it adds sever blank spaces to my array. Once I format the text with:
string formatted = string.Format("{2} {1} {0}", separate);
formatted = formatted.Replace(",", "");
Console.WriteLine("Dear " + formatted + ":");
the result needs to be:
Dear Mr. Bernie Sanders: ..etc
My best bet would be to use a regular expression.
var str = "Sanders,\tBernie M Democrat";
var whitespaces = new Regex(#"\s+");
str = whitespaces.Replace(str, " ");
var ar = str.Split(' ');
\s+ stands for 1 or more whitespaces, which will be replaced with just one whitespace, so you can easily split.
Another regular expression:
string[] wordArray = Regex.Split("This is \t my string of words!,", "[^a-zA-Z]+");
Here is an example. You will have to keep track of all the indexes.
Reference to regular expression chars and meanings.
https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx
Regex is really powerful once you get the hang of it.
Here I am using \s+ The \s is any whitespace character. The + makes it 'greedy'. Means match any whitespace character 1 or More times. You can use the * quantifier which means 0 or More times. Be careful with that one xD
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)
{
//extra spaces everywhere
string person = "Sanders, Bernie M Democrat\nBoehner, John M Republican";
var stripchars = new Regex(#"\s+");
person = stripchars.Replace(person, " ");
string[] line = person.Split('\n'); // spit on each new line
// temp vars to hold key pieces of information
string firstname = "";
string lastname = "";
string sex = "";
string party = "";
string formatted = "";
// the new line string
for (int i = 0; i < line.Length; i++ )
{
string[] tempCells = line[i].Split(' ');
//each cell in the line
for (int k = 0; k < tempCells.Length; k++)
{
firstname = tempCells[0].Replace(",", "");
lastname = tempCells[1];
sex = tempCells[2];
party = tempCells[3];
}
//updated to use tunrary
string malefemale = sex == "M" ? "Mr." : "Ms.";
formatted = "Dear " + malefemale + " " + lastname + " " + firstname + ":";
Console.WriteLine(formatted);
}
Console.Read();//pause
}
}
}
Output: Dear John Boehner:

Why is this Regex.Match failing?

I have this little method to look for the 3-digit number in a string and increment it by one. The types of strings I am passing in are like CP1-P-CP2-004-D and MOT03-C-FP04-003.
char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
foreach (char c in alphabet)
{
m = Regex.Match(s, #"\d{3}(?=[" + c + "-]|$)");
}
if (m.Success)
{
int i = Convert.ToInt32(m.Value); i += 1;
Console.WriteLine(s + " - " + i.ToString("D3"));
}
else { Console.WriteLine(s + " - No success"); }
EDIT: Initially I just had this; to test out my Regex.Match case:
Match m = Regex.Match(s, #"\d{3}(?=[A-]|$)");
And it worked with CP1PCP2001A no worries, but when I updated it, and tried CP1PCP2001C it returned "No Success", while CP1PCP2001 works no problem. Can anyone tell me why this is?
Have you tried
m = Regex.Match(s, #"\d{3}(?=[A-Z\-]|$)");
[A-Z] means that it can be any of the capital letters between A and Z thus eliminating the need for char[] alphabet, and the \- allows you to add the '-' as a parameter, without causing conflict with the first parameter.
From the comments, we're looking for "the first 3 digit number (coming from the right)". Here's a literal implementation:
m = Regex.Match(s, #"\d{3}", RegexOptions.RightToLeft);
This is more permissive towards unexpected characters than the other answers. You can decide whether that's good or bad for your application.
re-write the code this way
bool matched = false;
foreach (char c in alphabet)
{
m = Regex.Match(s, #"\d{3}(?=[" + c + "-]|$)");
if (m.Success)
{
int i = Convert.ToInt32(m.Value); i += 1;
Console.WriteLine(s + " - " + i.ToString("D3"));
matched=true;
break;
}
}
if(!matched)
Console.WriteLine(s + " - No success");
a better way would be not to loop and specify the char range to match in regex itself
example
m = Regex.Match(s, #"\d{3}(?=[A-Z\-]|$)");
if (m.Success)
{
int i = Convert.ToInt32(m.Value); i += 1;
Console.WriteLine(s + " - " + i.ToString("D3"));
}
else
Console.WriteLine(s + " - No success");
regex demo here

Replace all Special Characters including space with - using C#

I want to replace all Special Characters which can't be parse in URL including space, double space or any big space with '-' using C#.
I don't want to use any Parse Method like System.Web.HttpUtility.UrlEncode.
How to do this ? I want to include any number of space between two words with just one '-'.
For example, if string is Hello# , how are you?
Then, Result should be, Hello-how-are-you, no '-' if last index is any special character or space.
string str = "Hello# , how are you?";
string newstr = "";
//Checks for last character is special charact
var regexItem = new Regex("[^a-zA-Z0-9_.]+");
//remove last character if its special
if (regexItem.IsMatch(str[str.Length - 1].ToString()))
{
newstr = str.Remove(str.Length - 1);
}
string replacestr = Regex.Replace(newstr, "[^a-zA-Z0-9_]+", "-");
INPUT:
Hello# , how are you?
OUTPUT:
Hello-how-are-you
EDIT:
Wrap it inside a class
public static class StringCheck
{
public static string Checker()
{
string str = "Hello# , how are you?";
string newstr = null;
var regexItem = new Regex("[^a-zA-Z0-9_.]+");
if (regexItem.IsMatch(str[str.Length - 1].ToString()))
{
newstr = str.Remove(str.Length - 1);
}
string replacestr = Regex.Replace(newstr, "[^a-zA-Z0-9_]+", "-");
return replacestr;
}
}
and call like this,
string Result = StringCheck.Checker();
string[] arr1 = new string[] { " ", "#", "&" };
newString = oldString;
foreach repl in arr1
{
newString= newString.Replace(repl, "-");
}
Of course you can add into an array all of your spec characters, and looping trough that, not only the " ".
More about the replace method at the following link
You need two steps to remove last special character and to replace all the remaining one or more special characters with _
public static void Main()
{
string str = "Hello# , how are you?";
string remove = Regex.Replace(str, #"[\W_]$", "");
string result = Regex.Replace(remove, #"[\W_]+", "-");
Console.WriteLine(result);
Console.ReadLine();
}
IDEONE

How do I replace multiple spaces with a single space in C#?

How can I replace multiple spaces in a string with only one space in C#?
Example:
1 2 3 4 5
would be:
1 2 3 4 5
I like to use:
myString = Regex.Replace(myString, #"\s+", " ");
Since it will catch runs of any kind of whitespace (e.g. tabs, newlines, etc.) and replace them with a single space.
string sentence = "This is a sentence with multiple spaces";
RegexOptions options = RegexOptions.None;
Regex regex = new Regex("[ ]{2,}", options);
sentence = regex.Replace(sentence, " ");
string xyz = "1 2 3 4 5";
xyz = string.Join( " ", xyz.Split( new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries ));
I think Matt's answer is the best, but I don't believe it's quite right. If you want to replace newlines, you must use:
myString = Regex.Replace(myString, #"\s+", " ", RegexOptions.Multiline);
Another approach which uses LINQ:
var list = str.Split(' ').Where(s => !string.IsNullOrWhiteSpace(s));
str = string.Join(" ", list);
It's much simpler than all that:
while(str.Contains(" ")) str = str.Replace(" ", " ");
Regex can be rather slow even with simple tasks. This creates an extension method that can be used off of any string.
public static class StringExtension
{
public static String ReduceWhitespace(this String value)
{
var newString = new StringBuilder();
bool previousIsWhitespace = false;
for (int i = 0; i < value.Length; i++)
{
if (Char.IsWhiteSpace(value[i]))
{
if (previousIsWhitespace)
{
continue;
}
previousIsWhitespace = true;
}
else
{
previousIsWhitespace = false;
}
newString.Append(value[i]);
}
return newString.ToString();
}
}
It would be used as such:
string testValue = "This contains too much whitespace."
testValue = testValue.ReduceWhitespace();
// testValue = "This contains too much whitespace."
myString = Regex.Replace(myString, " {2,}", " ");
For those, who don't like Regex, here is a method that uses the StringBuilder:
public static string FilterWhiteSpaces(string input)
{
if (input == null)
return string.Empty;
StringBuilder stringBuilder = new StringBuilder(input.Length);
for (int i = 0; i < input.Length; i++)
{
char c = input[i];
if (i == 0 || c != ' ' || (c == ' ' && input[i - 1] != ' '))
stringBuilder.Append(c);
}
return stringBuilder.ToString();
}
In my tests, this method was 16 times faster on average with a very large set of small-to-medium sized strings, compared to a static compiled Regex. Compared to a non-compiled or non-static Regex, this should be even faster.
Keep in mind, that it does not remove leading or trailing spaces, only multiple occurrences of such.
This is a shorter version, which should only be used if you are only doing this once, as it creates a new instance of the Regex class every time it is called.
temp = new Regex(" {2,}").Replace(temp, " ");
If you are not too acquainted with regular expressions, here's a short explanation:
The {2,} makes the regex search for the character preceding it, and finds substrings between 2 and unlimited times.
The .Replace(temp, " ") replaces all matches in the string temp with a space.
If you want to use this multiple times, here is a better option, as it creates the regex IL at compile time:
Regex singleSpacify = new Regex(" {2,}", RegexOptions.Compiled);
temp = singleSpacify.Replace(temp, " ");
You can simply do this in one line solution!
string s = "welcome to london";
s.Replace(" ", "()").Replace(")(", "").Replace("()", " ");
You can choose other brackets (or even other characters) if you like.
no Regex, no Linq... removes leading and trailing spaces as well as reducing any embedded multiple space segments to one space
string myString = " 0 1 2 3 4 5 ";
myString = string.Join(" ", myString.Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries));
result:"0 1 2 3 4 5"
// Mysample string
string str ="hi you are a demo";
//Split the words based on white sapce
var demo= str .Split(' ').Where(s => !string.IsNullOrWhiteSpace(s));
//Join the values back and add a single space in between
str = string.Join(" ", demo);
// output: string str ="hi you are a demo";
Consolodating other answers, per Joel, and hopefully improving slightly as I go:
You can do this with Regex.Replace():
string s = Regex.Replace (
" 1 2 4 5",
#"[ ]{2,}",
" "
);
Or with String.Split():
static class StringExtensions
{
public static string Join(this IList<string> value, string separator)
{
return string.Join(separator, value.ToArray());
}
}
//...
string s = " 1 2 4 5".Split (
" ".ToCharArray(),
StringSplitOptions.RemoveEmptyEntries
).Join (" ");
I just wrote a new Join that I like, so I thought I'd re-answer, with it:
public static string Join<T>(this IEnumerable<T> source, string separator)
{
return string.Join(separator, source.Select(e => e.ToString()).ToArray());
}
One of the cool things about this is that it work with collections that aren't strings, by calling ToString() on the elements. Usage is still the same:
//...
string s = " 1 2 4 5".Split (
" ".ToCharArray(),
StringSplitOptions.RemoveEmptyEntries
).Join (" ");
Many answers are providing the right output but for those looking for the best performances, I did improve Nolanar's answer (which was the best answer for performance) by about 10%.
public static string MergeSpaces(this string str)
{
if (str == null)
{
return null;
}
else
{
StringBuilder stringBuilder = new StringBuilder(str.Length);
int i = 0;
foreach (char c in str)
{
if (c != ' ' || i == 0 || str[i - 1] != ' ')
stringBuilder.Append(c);
i++;
}
return stringBuilder.ToString();
}
}
Use the regex pattern
[ ]+ #only space
var text = Regex.Replace(inputString, #"[ ]+", " ");
I know this is pretty old, but ran across this while trying to accomplish almost the same thing. Found this solution in RegEx Buddy. This pattern will replace all double spaces with single spaces and also trim leading and trailing spaces.
pattern: (?m:^ +| +$|( ){2,})
replacement: $1
Its a little difficult to read since we're dealing with empty space, so here it is again with the "spaces" replaced with a "_".
pattern: (?m:^_+|_+$|(_){2,}) <-- don't use this, just for illustration.
The "(?m:" construct enables the "multi-line" option. I generally like to include whatever options I can within the pattern itself so it is more self contained.
I can remove whitespaces with this
while word.contains(" ") //double space
word = word.Replace(" "," "); //replace double space by single space.
word = word.trim(); //to remove single whitespces from start & end.
Without using regular expressions:
while (myString.IndexOf(" ", StringComparison.CurrentCulture) != -1)
{
myString = myString.Replace(" ", " ");
}
OK to use on short strings, but will perform badly on long strings with lots of spaces.
try this method
private string removeNestedWhitespaces(char[] st)
{
StringBuilder sb = new StringBuilder();
int indx = 0, length = st.Length;
while (indx < length)
{
sb.Append(st[indx]);
indx++;
while (indx < length && st[indx] == ' ')
indx++;
if(sb.Length > 1 && sb[0] != ' ')
sb.Append(' ');
}
return sb.ToString();
}
use it like this:
string test = removeNestedWhitespaces("1 2 3 4 5".toCharArray());
Here is a slight modification on Nolonar original answer.
Checking if the character is not just a space, but any whitespace, use this:
It will replace any multiple whitespace character with a single space.
public static string FilterWhiteSpaces(string input)
{
if (input == null)
return string.Empty;
var stringBuilder = new StringBuilder(input.Length);
for (int i = 0; i < input.Length; i++)
{
char c = input[i];
if (i == 0 || !char.IsWhiteSpace(c) || (char.IsWhiteSpace(c) &&
!char.IsWhiteSpace(strValue[i - 1])))
stringBuilder.Append(c);
}
return stringBuilder.ToString();
}
How about going rogue?
public static string MinimizeWhiteSpace(
this string _this)
{
if (_this != null)
{
var returned = new StringBuilder();
var inWhiteSpace = false;
var length = _this.Length;
for (int i = 0; i < length; i++)
{
var character = _this[i];
if (char.IsWhiteSpace(character))
{
if (!inWhiteSpace)
{
inWhiteSpace = true;
returned.Append(' ');
}
}
else
{
inWhiteSpace = false;
returned.Append(character);
}
}
return returned.ToString();
}
else
{
return null;
}
}
Mix of StringBuilder and Enumerable.Aggregate() as extension method for strings:
using System;
using System.Linq;
using System.Text;
public static class StringExtension
{
public static string CondenseSpaces(this string s)
{
return s.Aggregate(new StringBuilder(), (acc, c) =>
{
if (c != ' ' || acc.Length == 0 || acc[acc.Length - 1] != ' ')
acc.Append(c);
return acc;
}).ToString();
}
public static void Main()
{
const string input = " (five leading spaces) (five internal spaces) (five trailing spaces) ";
Console.WriteLine(" Input: \"{0}\"", input);
Console.WriteLine("Output: \"{0}\"", StringExtension.CondenseSpaces(input));
}
}
Executing this program produces the following output:
Input: " (five leading spaces) (five internal spaces) (five trailing spaces) "
Output: " (five leading spaces) (five internal spaces) (five trailing spaces) "
Old skool:
string oldText = " 1 2 3 4 5 ";
string newText = oldText
.Replace(" ", " " + (char)22 )
.Replace( (char)22 + " ", "" )
.Replace( (char)22 + "", "" );
Assert.That( newText, Is.EqualTo( " 1 2 3 4 5 " ) );
You can create a StringsExtensions file with a method like RemoveDoubleSpaces().
StringsExtensions.cs
public static string RemoveDoubleSpaces(this string value)
{
Regex regex = new Regex("[ ]{2,}", RegexOptions.None);
value = regex.Replace(value, " ");
// this removes space at the end of the value (like "demo ")
// and space at the start of the value (like " hi")
value = value.Trim(' ');
return value;
}
And then you can use it like this:
string stringInput =" hi here is a demo ";
string stringCleaned = stringInput.RemoveDoubleSpaces();
I looked over proposed solutions, could not find the one that would handle mix of white space characters acceptable for my case, for example:
Regex.Replace(input, #"\s+", " ") - it will eat your line breaks, if they are mixed with spaces, for example \n \n sequence will be replaced with
Regex.Replace(source, #"(\s)\s+", "$1") - it will depend on whitespace first character, meaning that it again might eat your line breaks
Regex.Replace(source, #"[ ]{2,}", " ") - it won't work correctly when there's mix of whitespace characters - for example "\t \t "
Probably not perfect, but quick solution for me was:
Regex.Replace(input, #"\s+",
(match) => match.Value.IndexOf('\n') > -1 ? "\n" : " ", RegexOptions.Multiline)
Idea is - line break wins over the spaces and tabs.
This won't handle windows line breaks correctly, but it would be easy to adjust to work with that too, don't know regex that well - may be it is possible to fit into single pattern.
The following code remove all the multiple spaces into a single space
public string RemoveMultipleSpacesToSingle(string str)
{
string text = str;
do
{
//text = text.Replace(" ", " ");
text = Regex.Replace(text, #"\s+", " ");
} while (text.Contains(" "));
return text;
}

Categories

Resources