Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to filter out an array based on 2 keywords that must be in order, So far i've got this:

string[] matchedOne = Array.FindAll(converList, s => s.Contains(split[1]));

string[] matchedTwo = Array.FindAll(matchedOne, s => s.Contains(split[2]));

if (matchedTwo.Length == 0)
{
    Console.Clear();
    Console.WriteLine("Sorry, Your Conversion is invalid");
    Main();
}

converlist =

ounce,gram,28.0
ounce,fake,28.0 - Fake one I added for examples
gram, ounce, 3.0 - Fake one I added for examples
pound,ounce,16.0
pound,kilogram,0.454
pint,litre,0.568
inch, centimetre,2.5
mile,inch,63360.0

If the user types in 5, ounce, gram, When passed through "matchedOne" it would find; "once,gram,28.0" and "ounce,fake,28.0" . not those, "pound,ounce,16.0" and "gram,ounce,3.0" as it does now.

Then in "matchedTwo" it would only find "once,gram,28.0" not that and "gram,ounce,3.0"

-- Just to add: I cant use anything over "system;".

share|improve this question
    
Have you considered using a class for this data, rather than comma-delimited list? –  What Would Be Cool Apr 16 '13 at 2:08

2 Answers 2

up vote 0 down vote accepted
var regex = new Regex(
      string.Format("^.*{0}.*{1}.*$",
      Regex.Escape(split[1]), Regex.Escape(split[2])),
      RegexOptions.IgnoreCase
      | RegexOptions.Multiline
      | RegexOptions.CultureInvariant
      | RegexOptions.IgnorePatternWhitespace
      | RegexOptions.Compiled
    );

Match m = regex.Match(converlist);

which basically just matches a line where split[1] comes before split[2]

or without regex

string[] match = Array.FindAll(matchedOne, s => s.IndexOf(split[1])==-1?false: s.IndexOf(split[2], s.IndexOf(split[1])) != -1);

and working conversion...

const string converlist = "ounce,gram,28.0\r\nounce,fake,28.0\r\ngram, ounce, 3.0\r\npound,ounce,16.0\r\npound,kilogram,0.454\r\npint,litre,0.568\r\ninch, centimetre,2.5\r\nmile,inch,63360.0\r\n";
var split = "5,ounce,gram".Split(new[] { ',' });
var list = converlist.Split(new[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries).ToList();
var matches = list.FindAll(s =>  s.IndexOf(split[1])==-1?false: s.IndexOf(split[2], s.IndexOf(split[1])) != -1);
var conversionLine = matches[0];
Console.WriteLine(conversionLine);
var conversionFactor = decimal.Parse(conversionLine.Split(new[] { ',' })[2]);
var valueToConvert = decimal.Parse(split[0].Trim());
Console.WriteLine(string.Format("{0} {2}s is {1} {3}s", valueToConvert, conversionFactor * valueToConvert, split[1], split[2]));
share|improve this answer
    
Thanks, But unfortunately I cant use system.text , But at least it comes in handy for other things. –  Kennyist Apr 16 '13 at 2:32
    
why not? its part of C#? might want to include any artificial limitations in your question –  Keith Nicholas Apr 16 '13 at 2:37
    
This is for an assignment where I cant use anything over "system;" and "system.io;". The program works since there are no other matches in "MatchedTwo". Just want to get some other features in, in-case some more get added to the .txt . –  Kennyist Apr 16 '13 at 2:43
    
@Kennyist updated my answer... –  Keith Nicholas Apr 16 '13 at 2:45
    
@Kennyist actually, slight problem, just updated again –  Keith Nicholas Apr 16 '13 at 2:50

Above answer satisfies what you need, however, this will also work.

string[] s = { "ounce,gram", "gram,ounce", "pound,carret" };

foreach (string temp in s.Where(x => (x.IndexOf("ounce")>-1) && (x.IndexOf("ounce") < x.IndexOf("gram"))))
            Debug.WriteLine(temp);
share|improve this answer
1  
if there is a conversion "pound,gram" this will fail as the index of "ounce" (-1) will be less than "gram" –  Keith Nicholas Apr 16 '13 at 2:55
    
Thanks for finding the bug, fixed it. –  Buddha Apr 16 '13 at 2:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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