tags:

views:

193

answers:

4

Java has a regex function called "LookingAt()" which will allow partial matches against patterns, my question is:

Does .net have an equivalent of "LookingAt()"?

I may or may not use it for KeyPress validation but I just would like to know for future reference.

Thanks in advance

Jon

+1  A: 

This may not be relevant to your case, but in general this is not a good idea because some languages often need combined keys to enter chars: a "â" is often typed by keying ^ and a. If you only want to allow alphanumeric keys and want to include â, you might prevent their entering if you disallow ^.

Thomas Tempelmann
Good point, however the application is an internal one and this won't be an issue.
wheelibin
A: 

You could use javascript to disable the key. You just have to find the keyCode for the key you are disabling this disables the return or enter key on the keyboard.

//This function disables the enter key so that the user can't enter a bunch of spaces. function kH(e) { var pK = e ? e.which : window.event.keyCode; return pK != 13; } document.onkeypress = kH; if (document.layers) document.captureEvents(Event.KEYPRESS);

Here is a page that lists all of the keycodes for your keyboard. 13 was for the enter key so in this function I am allowing all keys != to 13

http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001686.html

G Jason Smith
+1  A: 

Here is some code I use to check an email address with regex. I'm not sure if this is what you are looking for?

Regex emailregex = new Regex("(?<user>[^@]+)@(?<host>.+)");
        string s = "[email protected]";

        Match m = emailregex.Match(s);


        if (m.Success)
        {

        do something here


}
G Jason Smith
+2  A: 

I don't think lookingAt() is the correct model for what you're trying to do. All it does is anchor the match to the beginning of the input, as if you had used the ^ start anchor at the beginning of your regex and called find(), and that would be trivial to emulate in .NET.

I think what you're trying to do is prevent users from entering invalid data. For example, if a textfield expects a decimal number with two decimal places, your validation regex might be \d+\.\d\d . If a user starts by typing a digit, the regex doesn't match, but the digit could be part of a valid entry, so you let them keep on typing. But if the user types 'z', you block it because there's no way it could be part of a valid entry.

In other words, you don't want to know if the regex matches part of the string, you want to know if the string matches part of the regex. There is a way to do that in Java, using the hitEnd() method, but I don't think .NET has an equivalent for that. In fact, I don't know of any other regex flavor that does.

But blocking users' keypresses is rather rude anyway, in my opinion. I prefer to let them know by some other means that their input is invalid. For example, you can display the contents of the textfield in red, or disable the button that they use to submit the entry (or both). Each time something changes in the textfield, you check the contents against your (full) regex and update the state of the field and/or button accordingly.

Here's demonstration of hitEnd():

import java.util.regex.*;

public class Test
{
  public static void main(String[] args) throws Exception
  {
    Pattern p = Pattern.compile("\\d+\\.\\d\\d");
    Matcher m = p.matcher("");
    String[] tests = { "5.99", "5", "@" };
    for (String s : tests)
    {
      if (m.reset(s).matches())
      {
        System.out.println("full match");
      }
      else if (m.hitEnd())
      {
        System.out.println("partial match");
      }
      else 
      {
        System.out.println("no match");
      }
    }
  }
}

I get "full match" for the first string, "partial match" for the second, and "no match" for the third.

Alan Moore
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.html#lookingAt() - Thsi suggests that I could implement LookingAt for validation using a TextChanged event by providing a "so far so good" kind of response. To be honest I should never have mentioned keypress validation I simply...
wheelibin
...wanted to know if I could do a "so far so good" i.e. "the input is matching the pattern so far" feature using a LookingAt equivalent. I appreciate the advice on the KeyPress validation and will have a think about it...that is outside the scope of my question though to be honest!
wheelibin
"So far so good" is an excellent description of what hitEnd() does; check out the demo I added to my answer. lookingAt() effectively means the opposite: the whole pattern matches the first part of the string, and any characters beyond the matched text are ignored.
Alan Moore
Thanks for the example and the explanation of the difference.
wheelibin
@Alan: That's what I tried to explain to wheelibin all along. :-) +1 for the nice and complete answer.
Tomalak
@Tomalak, I wasn't asking for advice on how to design my validation though and you didn't actually answer my question whereas Alan did.....thanks for your input both of you.
wheelibin