RegularExpressions

J

Jaroslav Bucek

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
 
J

Jesse Houwing

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).)*$
 
J

Jesse Houwing

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...
 
E

Ethan Strauss

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
 
J

Jaroslav Bucek

Hello Jesse,
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...
 
J

Jaroslav Bucek

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.
 
M

Maate

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...)
 
J

Jesse Houwing

Hello Jaroslav,
Hello Jesse,

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/archive/2006/03/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'.
 
J

Jaroslav Bucek

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-5.10.0/pod/perlrequick.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/archive/2006/03/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top