Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Checking if string represents a valid regular expression


This question is answered.


Permlink Replies: 7 - Last Post: Dec 17, 2015 6:58 AM Last Post By: Robert Triest
Andrej Mrvar

Posts: 99
Registered: 10/20/10
Checking if string represents a valid regular expression  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 16, 2015 11:16 PM
Using:

if TRegEx.IsMatch(s1, s2) then ...

I can check if regular expression s2 can be matched in string s1.

But how can I check if string s2 is a valid regular expression (before matching it - to avoid error).

For example
*a
is not a valid regular expression (nothing to repeat), and I would like to avoil checking such an invalid expressions.

Thanks
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Checking if string represents a valid regular expression  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 16, 2015 11:48 PM   in response to: Andrej Mrvar in response to: Andrej Mrvar
I've never seen an example of that.

Maybe you can use a regular expression to check a regular expression.. ;-)
Andrej Mrvar

Posts: 99
Registered: 10/20/10
Re: Checking if string represents a valid regular expression  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 17, 2015 12:50 AM   in response to: Robert Triest in response to: Robert Triest
Thanks for your reply.

Maybe some more explanation why I use that:
User can input regular expression to search for matching in a longer list of strings.
But if user enters for example
*a
and expects that he will get all strings ending with letter 'a'
he gets an error message:
'Error in regular expression at offset 0: nothing to repeat'
and the program hangs, (what is not really nice ;(
Uwe Raabe

Posts: 180
Registered: 10/20/00
Re: Checking if string represents a valid regular expression
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 17, 2015 3:13 AM   in response to: Andrej Mrvar in response to: Andrej Mrvar
Andrej Mrvar wrote:

Thanks for your reply.

Maybe some more explanation why I use that:
User can input regular expression to search for matching in a longer
list of strings. But if user enters for example
*a
and expects that he will get all strings ending with letter 'a'
he gets an error message:
'Error in regular expression at offset 0: nothing to repeat'
and the program hangs, (what is not really nice ;(

If you want to check for those errors in advance, use something like
this:

function CheckRegEx(const Value: string): Boolean;
var
myRegEx: TRegEx;
begin
try
myRegEx := TRegEx.Create(Value, [roNotEmpty, roCompile]);
result := true;
except
result := false;
end;
end;

You may then even use that myRegEx variable inside your application
calling myRegEx.IsMatch(s1) instead of using TRegEx.IsMatch(s1, s2).

--
Uwe Raabe
Embarcadero MVP
Andrej Mrvar

Posts: 99
Registered: 10/20/10
Re: Checking if string represents a valid regular expression  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 17, 2015 4:02 AM   in response to: Uwe Raabe in response to: Uwe Raabe
Thanks a lot. That works and is exactly what I need ;)
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Checking if string represents a valid regular expression  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 17, 2015 3:50 AM   in response to: Andrej Mrvar in response to: Andrej Mrvar
But why you let the user add a regular expression?
Why not let the user type only characters and you handle the regular expression.
Are there that much options for the user to search?
If you for example use a database query you let the user add a string and
select a radiobutton like: Exact match, Part match.
And you are changing the user selection into a query. where fieldname like '%str%'.
I would try to do the same with using regular expressions.
Andrej Mrvar

Posts: 99
Registered: 10/20/10
Re: Checking if string represents a valid regular expression  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 17, 2015 4:03 AM   in response to: Robert Triest in response to: Robert Triest
You are right.

I will provide an option for entering regular expressions only for 'experienced users'
who know what regular expressions are...
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Checking if string represents a valid regular expression  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 17, 2015 6:58 AM   in response to: Andrej Mrvar in response to: Andrej Mrvar
I will provide an option for entering regular expressions only for 'experienced users'
Good idea but I hope they don't come to you too often to ask you to figure out to regular express a certain problem ;-)
(why is this not working?, Don't you support this or that option?, Andrej, how do I filter all valid postalcodes?)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02