Saturday, 1 November 2014

? : once or not at all

you can use metacharacter ? To match a character once or not at all.

For Example 'threat' 'treat' are same except that one has a h and the other doesn't, we can use 'th?reat' to match both. 

Sample Output
Enter Regular Expression
th?reat
Enter the string
treat threat
I found the text treat starting at index 0 Ending at index 5
I found the text threat starting at index 6 Ending at index 12
               
another example, some will type August, some will type Aug. Now we can match both using 'Aug(ust)?'.

Enter Regular Expression
Aug(ust)?
Enter the string
I can type Aug or August my regx match both
I found the text Aug starting at index 11 Ending at index 14
I found the text August starting at index 18 Ending at index 24


Note
Use this program to run above sample



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment