3

I have a string array string[] arr, which contains values like N36102W114383, N36102W114382 etc...
I want to split the each and every string such that the value comes like this N36082 and W115080.

What is the best way to do this?

3
  • 2
    What language? IMO you don't want to use regex for this. Commented Nov 8, 2011 at 7:38
  • 2
    something like this? (N\d+)(W\d+) or (N[0-9]+)(W[0-9]+) Commented Nov 8, 2011 at 7:38
  • Are the strings always the same length? Commented Nov 8, 2011 at 8:27

6 Answers 6

1

This should work for you.

Regex regexObj = new Regex(@"\w\d+"); # matches a character followed by a sequence of digits
Match matchResults = regexObj.Match(subjectString);
while (matchResults.Success) {
    matchResults = matchResults.NextMatch(); #two mathches N36102 and W114383
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you have the fixed format every time you can just do this:

string[] split_data = data_string.Insert(data_string.IndexOf("W"), ",")
    .Split(",", StringSplitOptions.None); 

Here you insert a recognizable delimiter into your string and then split it by this delimiter.

Comments

0

Forgive me if this doesn't quite compile, but I'd just break down and write the string processing function by hand:

public static IEnumerable<string> Split(string str)
{
    char [] chars = str.ToCharArray();
    int last = 0;
    for(int i = 1; i < chars.Length; i++) {
        if(char.IsLetter(chars[i])) {
            yield return new string(chars, last, i - last);
            last = i;
        }
    }

    yield return new string(chars, last, chars.Length - last);
}

Comments

0

If you use C#, please try:

String[] code = new Regex("(?:([A-Z][0-9]+))").Split(text).Where(e => e.Length > 0 && e != ",").ToArray();

Comments

0

in case you're only looking for the format NxxxxxWxxxxx, this will do just fine :

Regex r = new Regex(@"(N[0-9]+)(W[0-9]+)");

Match mc = r.Match(arr[i]);
string N = mc.Groups[1];
string W = mc.Groups[2];

Comments

0

Using the 'Split' and 'IsLetter' string functions, this is relatively easy in c#.

Don't forget to write unit tests - the following may have some corner case errors!

    // input has form "N36102W114383, N36102W114382"
    // output: "N36102", "W114383", "N36102", "W114382", ...
    string[] ParseSequenceString(string input)
    {
        string[] inputStrings = string.Split(',');

        List<string> outputStrings = new List<string>();

        foreach (string value in inputstrings) {
            List<string> valuesInString = ParseValuesInString(value);
            outputStrings.Add(valuesInString);
        }

        return outputStrings.ToArray();
    }

    // input has form "N36102W114383"
    // output: "N36102", "W114383"
    List<string> ParseValuesInString(string inputString)
    {
        List<string> outputValues = new List<string>(); 
        string currentValue = string.Empty;
        foreach (char c in inputString)
        {
            if (char.IsLetter(c))
            {
                if (currentValue .Length == 0)
                {
                    currentValue += c;
                } else
                {
                    outputValues.Add(currentValue);
                    currentValue = string.Empty;
                }
            }
            currentValue += c;
        }
        outputValues.Add(currentValue);
        return outputValues;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.