PC Review


Reply
Thread Tools Rate Thread

RegularExpressions

 
 
Jaroslav Bucek
Guest
Posts: n/a
 
      20th Feb 2008
Hi,

is it possible to create regular expression, using c# Regex to:

1. check if the string does NOT contain a word
2. check the same as 1. but for reverse word

Why:
I have to check when the password doesn't contain username nor reverse
username.

Example:
I've username: joe
The password must not contain "joe" nor "eoj".

I know I can use "joe+" regex, but this returns true in case the string
"joe" is present but I need to get false in that case.

Thank you in advance.

Jarda


 
Reply With Quote
 
 
 
 
Jesse Houwing
Guest
Posts: n/a
 
      20th Feb 2008
Hello Jaroslav,

> Hi,
>
> is it possible to create regular expression, using c# Regex to:
>
> 1. check if the string does NOT contain a word
> 2. check the same as 1. but for reverse word
> Why:
> I have to check when the password doesn't contain username nor reverse
> username.
> Example:
> I've username: joe
> The password must not contain "joe" nor "eoj".
> I know I can use "joe+" regex, but this returns true in case the
> string "joe" is present but I need to get false in that case.


You can use a negative look ahead for that:

^((?!nottheword|drowehtton).)*$

--
Jesse Houwing
jesse.houwing at sogeti.nl


 
Reply With Quote
 
 
 
 
Jesse Houwing
Guest
Posts: n/a
 
      20th Feb 2008
Hello Jaroslav,

> Hi,
>
> is it possible to create regular expression, using c# Regex to:
>
> 1. check if the string does NOT contain a word
> 2. check the same as 1. but for reverse word
> Why:
> I have to check when the password doesn't contain username nor reverse
> username.
> Example:
> I've username: joe
> The password must not contain "joe" nor "eoj".
> I know I can use "joe+" regex, but this returns true in case the
> string "joe" is present but I need to get false in that case.


Ohh by the way. The 'joe+' regex does indeed match of joe is anywhere in
the string, but so would just 'joe' as regex. The + at the end doesn't belong
in your expression at all...

--
Jesse Houwing
jesse.houwing at sogeti.nl


 
Reply With Quote
 
Ethan Strauss
Guest
Posts: n/a
 
      20th Feb 2008
I would think that the easiest thing would be to just check for the match and
act if it DOES occur.

if (!Regex.IsMatch(PossiblePassword, "Joe"))
{
//Report the problem to the user
}

but I think there is a way to do what you have asked with negative
lookaheads. I have never done this, but I think it might be something like

if (!Regex.IsMatch(PossiblePassword, "(?!Joe).*"))
{
//Report the problem to the user
}

take a look at http://www.themssforum.com/Csharp/Regex-negative/, it has
some discussion of negative lookaheads.

Hope this helps.
Ethan

"Jaroslav Bucek" wrote:

> Hi,
>
> is it possible to create regular expression, using c# Regex to:
>
> 1. check if the string does NOT contain a word
> 2. check the same as 1. but for reverse word
>
> Why:
> I have to check when the password doesn't contain username nor reverse
> username.
>
> Example:
> I've username: joe
> The password must not contain "joe" nor "eoj".
>
> I know I can use "joe+" regex, but this returns true in case the string
> "joe" is present but I need to get false in that case.
>
> Thank you in advance.
>
> Jarda
>
>
>

 
Reply With Quote
 
Jaroslav Bucek
Guest
Posts: n/a
 
      20th Feb 2008
Hello Jesse,

>> 1. check if the string does NOT contain a word
>> 2. check the same as 1. but for reverse word

>
> You can use a negative look ahead for that:
>
> ^((?!nottheword|drowehtton).)*$


Thanks a lot. That's exactly what I was looking for in case of 1.

Is there a way to reverse "nottheword" using regex? For instance, there is
some /reverse option in Perl. I don't know too much about it, I just saw it
some time ago. Is it possible in .NET?

Thank you...

Jaroslav

PS. I know that joe and joe+ are same - that was my mistake ;-) Anyhow thank
you...


 
Reply With Quote
 
Jaroslav Bucek
Guest
Posts: n/a
 
      20th Feb 2008
Hello Ethan,

> if (!Regex.IsMatch(PossiblePassword, "Joe"))
> {
> //Report the problem to the user
> }


I knew about such option, but I didn't want to do it this way. The reason
is, that there is a lot of policies for the password and all of these regex
returns true if it matches the pattern. Only this one should be checked in
another if clause.

Anyhow, thank you for your time.

Regards,
Jaroslav

PS. Jesse's solution is right for me.


 
Reply With Quote
 
Maate
Guest
Posts: n/a
 
      20th Feb 2008
I don't think there's any pretty way to solve this. I would just
reverse the string without regex:

String str = "test";
Char[] c = new Char[str.Length];
c = str.ToCharArray();
Array.Reverse(c);
String revStr = new String(c); //now, revStr = "tset"

Then go for the basics. Either:

String password = "my secret password";
if(password.IndexOf(str, StringComparison.OrdinalIgnoreCase) > 0 ||
password.IndexOf(revStr, StringComparison.OrdinalIgnoreCase) > 0)
{
//do something
}

Or: Regex.Match(password, str + "|" + revStr)

The problem is, that what you actually are asking for, is to dynamic
create the finite state machine, which in .NET is represented by a
regex pattern. That is, to solve your problem, you need to dynamic
create the pattern as done in the above example. (At least, that's my
analysis...)
 
Reply With Quote
 
Jesse Houwing
Guest
Posts: n/a
 
      20th Feb 2008
Hello Jaroslav,

> Hello Jesse,
>
>>> 1. check if the string does NOT contain a word
>>> 2. check the same as 1. but for reverse word

>> You can use a negative look ahead for that:
>>
>> ^((?!nottheword|drowehtton).)*$
>>

> Thanks a lot. That's exactly what I was looking for in case of 1.
>
> Is there a way to reverse "nottheword" using regex? For instance,
> there is some /reverse option in Perl. I don't know too much about it,
> I just saw it some time ago. Is it possible in .NET?


No there is no such option. There is a RegexOptions.RightToLeft, but that
tries to find teh given pattern from the back of the string going to the
front instead of the other wat around. The pattern itself is still LeftToRight.

I'm not sure, but my guess is that the Perl reverse option is similar to
the .NET one.

There's a nice discussion on string.reverse functions here:
http://weblogs.sqlteam.com/mladenp/a...3/19/9350.aspx

And if you're using .NET 3.5, you could write an extension method for string
to make it more logical.

> PS. I know that joe and joe+ are same - that was my mistake ;-) Anyhow
> thank you...


the pattern 'joe' and 'joe+' are certainly not the same, that was what I
was trying to tell you.

Where 'joe' would only match 'joe'; 'joe+' would also match 'joeeeeeeeeee'.

--
Jesse Houwing
jesse.houwing at sogeti.nl


 
Reply With Quote
 
Jaroslav Bucek
Guest
Posts: n/a
 
      21st Feb 2008
> I'm not sure, but my guess is that the Perl reverse option is similar to
> the .NET one.


Try to look at
http://search.cpan.org/~rgarcia/perl...erlrequick.pod
There is an example:

# reverse all the words in a string
$x = "the cat in the hat";
$x =~ s/(\w+)/reverse $1/ge; # $x contains "eht tac ni eht tah"

Anyhow, it doesn't matter already.

> There's a nice discussion on string.reverse functions here:
> http://weblogs.sqlteam.com/mladenp/a...3/19/9350.aspx


For now I am reversing the string using C# method.

> And if you're using .NET 3.5, you could write an extension method for
> string to make it more logical.


No, I am using 2.0. Anyhow I will check that extensions.

> the pattern 'joe' and 'joe+' are certainly not the same, that was what I
> was trying to tell you.
>
> Where 'joe' would only match 'joe'; 'joe+' would also match
> 'joeeeeeeeeee'.


You are right. As I said, I am not the regex expert ;-)

Thank you for your time.

Jaroslav


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.Text.RegularExpressions Jeroen Vandezande Microsoft Dot NET Framework 2 10th May 2005 10:11 PM
Validating Email address with System.Text.RegularExpressions.RegEx Doug Microsoft Dot NET Framework 0 13th Aug 2004 07:35 PM
Validating Email address with System.Text.RegularExpressions.RegEx Doug Microsoft Dot NET Framework 3 13th Aug 2004 08:35 AM
System.Text.RegularExpressions.Regex Mike Labosh Microsoft Dot NET Framework 1 20th May 2004 12:45 AM
Logical "or" with RegularExpressions Microsoft ASP .NET 3 30th Dec 2003 03:00 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:29 PM.