473,698 Members | 2,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Partial Match with a .NET regular expression

Hi,

I would like to check for a partial match between an input string and a
regular expression using the Regex class in .NET. By partial match, I mean
that the input string could not yet be complete but I want to know if a
match is possible so far.

For instance I want to design a text box to enter a date and validate the
correctness of the date as the user types character. If the user enters
1953/12/23 it will match my regex of course but I want to check if there is
a potential match after each character is entered by the user. Trying the
match for "1", "19", "195",... 1953/12/2" should tell me that the string
doesn't match but could match ! If I try to validate "1953/16", then I would
get an error because this string will never match the regex even if it's not
yet complete.

Is there a way to do this with .NET regular expressions ?

Christian
Nov 15 '05 #1
2 7946
On Tue, 2 Sep 2003 15:13:19 +0200, "Christian Staffe" <x@y.z> wrote:
Hi,

I would like to check for a partial match between an input string and a
regular expression using the Regex class in .NET. By partial match, I mean
that the input string could not yet be complete but I want to know if a
match is possible so far.


I don't thing you can use RegEx for this... A regular expression
delivers either a match or no match...
You are trying to ask "0 or 1?" with only these answers as being
valid, but also accepting "0.5!" as an answer...

--
NULL
Nov 15 '05 #2

It seems to me that you would need to write the regex to accept each
intermediate state. Here is a simplified expression that will evaluate to
true for any intermediate state of a string in the format 1234/56/78, but it
doesn't have any date validation logic built in:

^(\d(\d(\d(\d(/(\d(\d(/(\d(\d)?)?)?)?) ?)?)?)?)?)?$
The key is to make sure that everything is optional, but each section can
only be matched if what must come before it is matched.

If you need to accept multiple date formats (mm/dd/yy, dd/mm/yyyy, etc.)
then you would probably just want to develop each with its own expression
and then use alternation to make one regex: <expression 1>|<expression
2>|<expression 3>...

Another possible way to do it is to use multiple expressions for the
in-between states and decide which one to use based on the length of the
input or the presence/position of certain characters. This results in
several more simple expressions, which may or may not be easier to code and
test than constructing one super-regex.

Brian Davis
www.knowdotnet.com
"Christian Staffe" <x@y.z> wrote in message
news:bj******** **@news.mch.sbs .de...
Hi,

I would like to check for a partial match between an input string and a
regular expression using the Regex class in .NET. By partial match, I mean
that the input string could not yet be complete but I want to know if a
match is possible so far.

For instance I want to design a text box to enter a date and validate the
correctness of the date as the user types character. If the user enters
1953/12/23 it will match my regex of course but I want to check if there is a potential match after each character is entered by the user. Trying the
match for "1", "19", "195",... 1953/12/2" should tell me that the string
doesn't match but could match ! If I try to validate "1953/16", then I would get an error because this string will never match the regex even if it's not yet complete.

Is there a way to do this with .NET regular expressions ?

Christian

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1935
by: Follower | last post by:
Hi, I am working on a function to return extracts from a text document with a specific phrase highlighted (i.e. display the context of the matched phrase). The requirements are: * Match should be case-insensitive, but extract should have case preserved.
1
3977
by: Venkat | last post by:
Hi, I am using match function of string to find if a character is there in a string. The function Match is working fine with all the other characters except when the searching character is "+". Here is the piece of code i am using var line1 = "Hell+O";
19
2152
by: Tom Deco | last post by:
Hi, I'm trying to use a regular expression to match a string containing a # (basically i'm looking for #include ...) I don't seem to manage to write a regular expression that matches this. My (probably to naive) approach is: p = re.compile(r'\b#include\b) I also tried p = re.compile(r'\b\#include\b) in a futile attempt to use a backslash as escape character before the #
12
1830
by: Jed Ozone | last post by:
New to Regex and I'm having a hard time figuring this one out. I need a regular expression what will based on balanced square brackets. For example: ], ,CC], ]] I'm trying to write a reg ex that will parse the above into 3 pieces: 1) ] (or $AA would be fine) 2) ,CC] 3) ]]
1
2056
by: tdmailbox | last post by:
I have the following regular expression. It works fine if the regex code returns a match. However if not the .match code fails. How can I code this so that it skips the match if the regular expression does not find anything? Regex reg_unit_num = new Regex("L_unit_num.*?>(.*?)</td>", RegexOptions.IgnoreCase);
38
15956
by: Steve Kirsch | last post by:
I need a simple function that can match the number of beginning and ending parenthesis in an expression. Here's a sample expression: ( ( "john" ) and ( "jane" ) and ( "joe" ) ) Does .NET have something built-in that can accomplish this, or do I have to write my own parser? I don't want to reinvent the wheel if possible.
6
9064
by: likong | last post by:
Hi, Any idea about how to write a regular expression that matches a substring xxx as long as the string does NOT contain substring yyy? Thanks. Kong
9
7354
by: a | last post by:
I need to write a regular expression to match a quoted string in which the double quote character itself is represented by 2 double quotes. For example: "beginning ""nested quoted string"" end" Any idea how to write this in boost::xpressive or boost::regex. Thanks,
14
4981
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any text</aClosingTag> I need a Regex that will get all of the text between the html tags above (the html tags are random and i do not know them before hand). The match string always starts with at least 5 digits.
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.