0

I have text document with this pattern:

Red fox got number1 socks: number2 and: number3

Red fox got number4 socks: number5 and: number6

Red fox got number7 socks: number8 and: number9 ...

I need to extract all numberx from each line and put it in array like:

[number1,number2,number3]

[number4,number5,number6]

[number7,number8,number9]

i'm using C#.

thanks

7
  • You want a IEnumerable<int[2]>? Commented Mar 19, 2012 at 12:27
  • What have you tried? Post some code. Why doesn't it work? Show us where and how it fails. Are you just asking us to write the code for you? Commented Mar 19, 2012 at 12:28
  • It can be any type of array which i can loop over. Commented Mar 19, 2012 at 12:29
  • @Ben Robinson, i don't know regular expressions at all. What do you suggest me to try ? Commented Mar 19, 2012 at 12:31
  • when you write number1 do you mean the string "number1" or the int 1? Commented Mar 19, 2012 at 12:32

4 Answers 4

1

your regex is: "Red fox got ([0-9]+) socks: ([0-9]+) and: ([0-9]+)"

Sign up to request clarification or add additional context in comments.

1 Comment

Alex how do i distinguish between match1 and match3 ?
1

Solution without regex:

string input = "Red fox got 1 socks: 22 and: 333";

string[] split = input.Split(' ');

string output = string.Format("[{0},{1},{2}]", split[3], split[5], split[7]);

Put the logic in a loop and for each input add the output to a list or array.

1 Comment

+1 for creative solution without Regex, never seen it comming.
0
class Program
{
    static void Main()
    {
        var input = File.ReadAllLines("test.txt");
        var result = input
            .Select(
                line => Regex.Matches(line, @"\d+")
                             .OfType<Match>()
                             .Select(m => m.Groups[0].Value)
            );
        foreach (string[] numbers in result)
        {
            Console.WriteLine(string.Join(",", numbers));
        }
    }
}

Given the following input file:

Red fox got 1 socks: 2 and: 3
Red fox got 4 socks: 5 and: 6
Red fox got 7 socks: 8 and: 9

the program prints:

1,2,3
4,5,6
7,8,9

Comments

0

Here you go

using System.Text.RegularExpressions;

foreach(var match in Regex.Matches(theText, "[0-9]+")
{
    var number = int.Parse(match.Value);
}

will allow you to iterate all numbers in the text. If you want them in triplets,

IEnumerable<Tuple<int,int,int>> NumberTripletsFromText(string theText)
{
    var results = new List<int>;
    var count = 0;

    foreach(var match in Regex.Matches(theText, "[0-9]+")
    {
        count++;
        results.Add(int.Parse(match.Value);

        if (count == 3)
        {
            yield return Tuple.Create(results[1], results[2], results[3]);
            results.Clear;
            count = 0;
        }
    }

    if (results > 0)
    {
       //Handle non divisible by three.
    }
}

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.