-1

I am try to split the following string in to 3 parts:

Esmael20170101one => Esmael 20170101 one

What are the options?

2
  • 2
    You should provide more details.
    – jason
    May 29, 2017 at 14:23
  • 1
    What is condition on which you want to split? When letter meets number? May 29, 2017 at 14:26

3 Answers 3

6

I suggest matching instead of splitting:

  string source = "Esmael20170101one";

  var match = Regex.Match(source, 
    @"^(?<name>[A-Za-z]+)(?<code>[0-9]+)(?<suffix>[A-Za-z]{3})$");

  string name = match.Groups["name"].Value;
  string code = match.Groups["code"].Value;
  string suffix = match.Groups["suffix"].Value;

if you insist on Regex.Split:

  string[] items = Regex.Split(source, "([0-9]+)");

  string name = items[0];
  string code = items[1];
  string suffix = items[2]; 
8
  • Both of these can fail on an edge case where the name (at the end) or the suffix (at the beginning) happens to have a number in it
    – ΩmegaMan
    May 29, 2017 at 15:27
  • @OmegaMan: quite right, that's why more details should be provided (please, see comments to the question). If your hypothesis (that 20170101 is in fact 1 Jan 2017) is correct one, then your solution is a better one; +1 for the observation May 29, 2017 at 15:33
  • You have both examples though. +1
    – ΩmegaMan
    May 29, 2017 at 15:39
  • The answer Dmitry is suggesting was ok for this case.Here is specification in detail: The first part is always string, the middle part is nummeric with any length and the last part could be any string with length of 3 characters.
    – Esmael
    May 29, 2017 at 19:47
  • @Esmael: Next time, please, start with the specification ;) May 29, 2017 at 19:50
2

The regular expression to use is ([a-zA-Z]*)(\d+)([a-zA-Z]*)

string input = "Esmael20170101one";
var match = new Regex("([a-zA-Z]*)(\\d+)([a-zA-Z]*)").Match(input);
if (match.Success) {
    Console.WriteLine(match.Groups[1].ToString());
    Console.WriteLine(match.Groups[2].ToString());
    Console.WriteLine(match.Groups[3].ToString());
}
Console.Read();
1

If you use regex, you can define what areas to capture. For example it appears that the middle component is a date, so why not specify what the date pattern is such as

^                # Beginning of String
(?<Name>[^\d]+)  # Capture to `Name`
(?<Date>\d{8})   # Capture to `Date`
(?<Note>.+)      # Capture to `Note`
$                # End of string

Because I have commented this you will need to use the pattern only option of IgnorePatternWhitespace which just tells the parser to strip the comments (#) out.

The result will be this in a single match

enter image description here

  • Group[0] has the whole thing matched.
  • Group["Name"] or Group[1] is the name that is found.
  • Group["Date"] or Group[2] is the date that is found.
  • Group["Note"] or Group[3] is the note which is found.

As Dmitry pointed out, we need more information. All of these patterns can fail if there are numbers found in either of the groups depending on their location. If you know that all dates are within the 21st century adjust my pattern to be (?<Date>20\d{6}) to make sure that a true date is captured in that field; though it is not foolproof.

2
  • Here is specification in detail: The first part is always string, the middle part is nummeric with any length and the last part could be any string with length of 3 characters.
    – Esmael
    May 29, 2017 at 19:53
  • @Esmael why didn't you put that in originally?
    – ΩmegaMan
    May 29, 2017 at 23:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.