5

Is there a way to determine that a single char is valid when a regular expression expects a specific number of that char?

I have a WPF custom keyboard and would like to adjust each key's availability based on a regular expression. This will work well when the expression is fairly simple and does not expect specific order of the chars or a specific length to satisfy the pattern.

However, when the pattern becomes more complex and specific, testing a single char against it will always fail.

For instance, given the regular expression [a-zA-Z0-9]{4}

These values will succeed:

  • ABCD
  • abcd
  • 1234
  • A23e

The expression clearly expects alphanumerical chars only. I would like a method that given the expression will reject special char, say "%", but accept "a" as "a" is acceptable in [a-zA-Z0-9]. The only issue is the specific length that will not be satisfied.

I am currently using Regex.IsMatch. I guess I am looking for a partial match testing method.

15
  • 3
    I have read your question twice and I still don't get what you want to do. Maybe just replace everything that's not alphanumeric [^a-zA-Z0-9]+? Commented Feb 11, 2015 at 22:20
  • @HamZa I read it as "is there any chance that substring I have will eventually (when added more characters) satisfy given regex". Commented Feb 11, 2015 at 22:22
  • 1
    Louis - are there any restrictions on regular expression (like "always in a form of ....") or you want that to check for partial patch against generic regex? Commented Feb 11, 2015 at 22:24
  • 2
    @HamZa but "ab%d" wil fail due to wrong char, not length - but getting generic enough filler string probably is way too complex (completely correct one)... Anyway I don't believe there is any sane way to get partial match for generic regex - parsing/reconstructing may be something to look into for entertainment purposes (but not really useful for real code) Commented Feb 11, 2015 at 22:33
  • 1
    @Alexei there is a sane way actually - see my answer ;) Commented Feb 11, 2015 at 22:56

1 Answer 1

5

Sure, you can, but not using the built-in regex engine unfortunately. You can use PCRE instead, which provides the partial matching feature you're asking for.

From the PCRE docs:

In normal use of PCRE, if the subject string that is passed to a matching function matches as far as it goes, but is too short to match the entire pattern, PCRE_ERROR_NOMATCH is returned. There are circumstances where it might be helpful to distinguish this case from other cases in which there is no match.

Consider, for example, an application where a human is required to type in data for a field with specific formatting requirements. An example might be a date in the form ddmmmyy, defined by this pattern:

 ^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$

If the application sees the user's keystrokes one by one, and can check that what has been typed so far is potentially valid, it is able to raise an error as soon as a mistake is made, by beeping and not reflecting the character that has been typed, for example. This immediate feedback is likely to be a better user interface than a check that is delayed until the entire string has been entered. Partial matching can also be useful when the subject string is very long and is not all available at once.

PCRE supports partial matching by means of the PCRE_PARTIAL_SOFT and PCRE_PARTIAL_HARD options, which can be set when calling any of the matching functions. For backwards compatibility, PCRE_PARTIAL is a synonym for PCRE_PARTIAL_SOFT. The essential difference between the two options is whether or not a partial match is preferred to an alternative complete match, though the details differ between the two types of matching function. If both options are set, PCRE_PARTIAL_HARD takes precedence.


But PCRE is a C library... So I've built a PCRE wrapper for .NET.

Usage example from the readme:

var regex = new PcreRegex(@"(?<=abc)123");
var match = regex.Match("xyzabc12", PcreMatchOptions.PartialSoft);
// result: match.IsPartialMatch == true

A little caution though: the wrapper is currently at v0.3, using PCRE v8.36 but PCRE v10.0 was released recently (with a new API), so expect some breaking changes in the API of v0.4 of PCRE.NET. The behavior should stay the same though.

And also, you should be aware of the differences between .NET and PCRE regex flavors. This should not be a problem for most cases though.

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

1 Comment

Thanks for your detailed answer Lucas. I will look into it later. For now, since I have a fairly small list of regular expressions to support, I will probably create a physical file that will map which keys are enabled based on each expression. This should be dynamic enough for the time being....

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.