• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

regular expressions - only part of what I am “looking for”

I am trying to write some code using regular expressions code that will find what I am “looking for” within some “input”.  The catch is that the “input” can contain only part of what I am “looking for”.  Also, what I am “looking for” can contain special characters but I am suppressing them with backslash, got this part working.

What do I need to add to the “pattern” to accomplish this?
 
string input = "This document is the [ABC ";
            string lookingfor = "[ABC:Name]";

            //suppress the special characters within the value
            string pattern = lookingfor.Replace(":", "\\:"); 
            pattern = pattern.Replace("[", "\\[");
            pattern = pattern.Replace("]", "\\]");

            MatchCollection list = Regex.Matches(input, "^[" + pattern + "]", RegexOptions.IgnoreCase);
            if (list.Count > 0) Console.WriteLine("Found It");

Open in new window

0
tampsystems
Asked:
tampsystems
  • 5
  • 4
  • 4
2 Solutions
 
Todd GerbertIT ConsultantCommented:
1) The colon and closing square bracket don't need to be escaped (http://msdn.microsoft.com/en-us/library/4edbef7e.aspx).

2) Your input contains "Blah blah [ABC" but your pattern is specifically looking for "[ABC:Word]" - that is not a match.  If you want your pattern to match the string "input" in your example, it needs to be shortened - "\[[A-Z]{3}\b" will match any occurence of an opening square bracket followed by three upper-case letters, followed by a word boundary.

So "This document is the [ABC " matches.

"This document is the [ABCD " does not match (too many letters).

http://msdn.microsoft.com/en-us/library/az24scfc.aspx
0
 
tampsystemsAuthor Commented:
The problem is that the user can define what I am “looking for”.  I am trying to take that and add some regular expressions to it so I can find a match.  The input can be a number of things.  The toughest one is that it can contain only part of what I am “looking for” i.e. “This document is the [ABC “

Can this be done?  Maybe regular expression is the wrong choice for this scenario.  
0
 
Todd GerbertIT ConsultantCommented:
I think you will need to more narrowly define your criteria.  "The quick Brown fox jumped over the lazy dog" has a B, which is part of [ABC, so should that match?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
tampsystemsAuthor Commented:
The requirement is that I need to be able to match the following scenario.
Input:      “The quick Brown fox jump”
Pattern:         “jumped”

0
 
käµfm³d 👽Commented:
I agree with tgerbert: regex looks for an exact match. However, you can try this and see if it suits you.

static string GetModifiedPattern(string pattern)
{
    System.Text.StringBuilder result = new System.Text.StringBuilder(pattern);

    for (int i = pattern.Length - 1; i > 0; i--)
    {
        result.AppendFormat("@{0}", pattern.Substring(0, i));
    }

    pattern = Regex.Escape(result.ToString());

    return pattern.Replace('@', '|');
}

Open in new window


Usage
string input = "This document is the [ABC ";
string lookingfor = "[ABC:Name]";

//suppress the special characters within the value
string pattern = GetModifiedPattern(lookingfor);

MatchCollection list = Regex.Matches(input, pattern, RegexOptions.IgnoreCase);

if (list.Count > 0)
{
    Console.WriteLine("Found It");
    Console.WriteLine(list[0].Value);
}

Console.ReadKey();

Open in new window

0
 
käµfm³d 👽Commented:
Take heed, though, I used @ as a "special" delimiter. If you original pattern contains an @ symbol, then you'll need to choose some other unique character to act as that delimiter. However, you'll want to stay away from any special regex characters as the call to Regex.Escape() will wreak havoc on the resulting pattern. This is why I didn't just use a vertical bar for the special delimiter.
0
 
käµfm³d 👽Commented:
Also, the longer your "looking for" string is, the longer the engine may take to run, depending also on the target "to search" string.
0
 
Todd GerbertIT ConsultantCommented:
The requirement is that I need to be able to match the following scenario.
Input:      “The quick Brown fox jump”
Pattern:         “jumped”

I understand that - and I assume you're thinking that "jump" should match "jumped" because they're both forms of the same verb, unfortunately pattern matching doesn't really work according to English grammatical rules. My suggestion is to not let the user make such matches. ;)

The opposite, with input set to "The quick brown fox jumped" and the pattern set to "jump", makes more sense and is much more easily achievable.
0
 
tampsystemsAuthor Commented:
Kaufmed, Your solution is working, I have to do some more testing.  

The only special characters that are allowed other then space and alphanumeric are the following:
_:;-/{}|[]

Should I just escape them with the backslash prior to calling the GetModifiedPattern method?
0
 
käµfm³d 👽Commented:
Should I just escape them with the backslash prior to calling the GetModifiedPattern method
You shouldn't have to. I inserted Regex.Escape() inside of that method which should take care of escaping all regex-related special characters, of which @ is not a member  = )
0
 
Todd GerbertIT ConsultantCommented:
Note that with input like "This [actual] document might contain: [ABC " your pattern is going to match the "[a" in "[actual]", not "[ABC", hence my suggestion to more narrowly define your criteria stands.
0
 
käµfm³d 👽Commented:
...besides, if you do that, the backslashes themselves will be escaped, and then you'll have extra "possible matches" in your pattern.
0
 
tampsystemsAuthor Commented:
It turned out that you were both right, i was able to use  the method provided buti still ran into trouble with the matching.  one day i will have to come back to this problem. thanks for your help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now