views:

139

answers:

4
+1  Q: 

String matching.

How to match the string "Net        Amount" (between Net and Amount there can be any number of spaces, including zero) with net amount?

Spaces between these two words can be any whitespace and exact matching of two string should be there. But Net Amount (first string with spaces) can be part of any string like Rate Net Amount or Rate CommissionNet Amount.

The match should be case-insensitive.

A: 

You can use a Regular Expression: Net.*Amount.

using System.Text.RegularExpressions;
Regex regex = new Regex("Net.*Amount");
String s = "Net     Amount";
Match m = emailregex.Match(s);

// Now you have information in m about the matching string.
Anders Abel
@Anders Abel, Will it match to the All the words which starts with Net ?
Harikrishna
Yes, it will if you use . before the *. The dot matches any character and the star says any number of occurences. If you use "Net +Amount" instead it will only match if there are at least one space (and no other characters) between the words.
Anders Abel
+5  A: 

Use regular expressions. Have a look at the System.Text.RegularExpressions namespace, namely the Regex class:

var regex = new RegEx("net(\s+)amount", RegexOptions.IgnoreCase);
//                    ^^^^^^^^^^^^^^^
//                        pattern

The argument string is what is called the regular expression pattern. Regular expression patterns describe what strings will match against it. They are expressed with a specialized syntax. Google for regular expressions and you should find plenty of information about regexes.

Usage example:

bool doesInputMatch = regex.IsMatch("nET      AmoUNT", );
//                                  ^^^^^^^^^^^^^^^^^
//                                     test input
stakx
Why the downvote? I don't mind, but I'm always willing to better myself (or at least my answers on SO ;).
stakx
@stakx: I upvoted your answer because it was a good suggestion at the time you posted it and with extra information. But the question has been so heavily edited and clarified that your answer cannot be considered a correct answer with the question's current form. I can imagine that someone reading the question now might downvote it for this reason. Note: I don't think it should downvoted for this reason - I'm just guessing why it might have been.
Mark Byers
@Mark Byers, thanks for the hint!
stakx
+4  A: 

If you just want to check if a match exists, use IsMatch:

using System;
using System.Text.RegularExpressions;

class Program
{
    public static void Main()
    {
        string s = "Net     Amount";
        bool isMatch = Regex.IsMatch(s, @"Net\s*Amount",
                                     RegexOptions.IgnoreCase);
        Console.WriteLine("isMatch: {0}", isMatch);
    }
}

Update: In your comments it sounds like the string you want to search for is only known at runtime. You could try building the regular expression dynamically, for example something like this:

using System;
using System.Text.RegularExpressions;

class Program
{
    public static void Main()
    {
        string input = "Net     Amount";
        string needle = "Net Amount";

        string regex = Regex.Escape(needle).Replace(@"\ ", @"\s*");
        bool isMatch = Regex.IsMatch(input, regex, RegexOptions.IgnoreCase);
        Console.WriteLine("isMatch: {0}", isMatch);
    }
}
Mark Byers
@Mark Byers,Thank You Very Much Sir....And I am really now big fan of you.
Harikrishna
@Mark Byers,Sir, if we want to match the string like buy-sell and buy/sell then how can we do it ?
Harikrishna
@Harikrishna: This is too far offtopic for this question IMHO. You should create a new question. Remember to include very clear specifications, example inputs, example outputs. Include examples both of when you want the match to succeed, and when you want it to fail. Attempt to solve the problem yourself first, and include your code in the question. Show an example of where and how your code fails.
Mark Byers
+2  A: 

You can use

Regex.IsMatch(SubjectString, @"net\s*amount", RegexOptions.Singleline | RegexOptions.IgnoreCase);
Hun1Ahpu
+1 For thinking about case-sensitivity. Note though that `RegexOptions.Singleline` is not necessary here - it affects the dot `.` only, and you have no dots in your regular expression.
Mark Byers