regular expression @"ta.*y";

T

Tony Johansson

Hi!

How can this string "tartvfgby" match this regular expression @"ta.*y";
I mean * means that preceding character may be repeated 0 or more times.
A dot means any single character except the newline character.

So I mean a dot can only take one character but it can repeat the same
because we have a * after.
For example if we have ra*t this will match rat, raat, raaaaaat but no rt or
radfgt

So there must be some strange meaning when we have .*

//Tony
 
F

Fred Mellender

Tony Johansson said:
Hi!

How can this string "tartvfgby" match this regular expression @"ta.*y";
I mean * means that preceding character may be repeated 0 or more times.
A dot means any single character except the newline character.

So I mean a dot can only take one character but it can repeat the same
because we have a * after.
For example if we have ra*t this will match rat, raat, raaaaaat but no rt
or radfgt

So there must be some strange meaning when we have .*

//Tony

You should take the .* to mean "repeat the '.' 0 or more times", not repeat
the character that matched the dot one or more times. Thus it is as if you
had "tay" and "ta.y" and "ta..y" and so forth. One of those (infinitely
many) regular expressions would match "tartvgby".
 
F

Family Tree Mike

Hi!

How can this string "tartvfgby" match this regular expression @"ta.*y";
I mean * means that preceding character may be repeated 0 or more times.
A dot means any single character except the newline character.

So I mean a dot can only take one character but it can repeat the same
because we have a * after.
For example if we have ra*t this will match rat, raat, raaaaaat but no rt or
radfgt

So there must be some strange meaning when we have .*

//Tony

* does not repeat "the" character, but rather the item before it, which
is the symbol for "any character". In other words, your pattern means
to match ta.y or ta..y or ta....y or tay or however many dots are needed
to find a match with a 'ta' at the start and a 'y' at the end.

your ra*t is a wildcard pattern for 'r' then multiple 'a' (0..n)
characters, then a 't'.
 
F

Family Tree Mike

* does not repeat "the" character, but rather the item before it, which
is the symbol for "any character". In other words, your pattern means to
match ta.y or ta..y or ta....y or tay or however many dots are needed to
find a match with a 'ta' at the start and a 'y' at the end.

your ra*t is a wildcard pattern for 'r' then multiple 'a' (0..n)
characters, then a 't'.

Sorry, the first sentence should be rewritten as follows:

'*' does not repeat "the matched" chracter, but rather the symbol before
it, which is the symbol for "any character".
 
A

Arne Vajhøj

How can this string "tartvfgby" match this regular expression @"ta.*y";
I mean * means that preceding character may be repeated 0 or more times.
A dot means any single character except the newline character.

So I mean a dot can only take one character but it can repeat the same
because we have a * after.
For example if we have ra*t this will match rat, raat, raaaaaat but no rt or
radfgt

So there must be some strange meaning when we have .*

Other have already explained the specific problem, but let
me add some general comments.

Regex is a very powerful tool that any developer should know, but
the syntax is a bit cryptic.

That means that you need to read about them.

One place to start is:

http://www.regular-expressions.info/tutorial.html

I can also provide you with:
- dozens of examples in C#
- link to some good articles in Danish (which I assume that
you will be able to read)

Drop me an email, if you want this stuff.

Arne
 
T

Tom Shelton

Other have already explained the specific problem, but let
me add some general comments.

Regex is a very powerful tool that any developer should know, but
the syntax is a bit cryptic.

That means that you need to read about them.

One place to start is:

http://www.regular-expressions.info/tutorial.html

I can also provide you with:
- dozens of examples in C#
- link to some good articles in Danish (which I assume that
you will be able to read)

Drop me an email, if you want this stuff.

Arne

And get a copy of Expresso - it is a great tool for experimenting with regex.
 
H

Helmut Giese

Hi!

How can this string "tartvfgby" match this regular expression @"ta.*y";
I mean * means that preceding character may be repeated 0 or more times.
A dot means any single character except the newline character.

So I mean a dot can only take one character but it can repeat the same
because we have a * after.

So there must be some strange meaning when we have .*
Hi Tony,
nothing strange there - just the inherent logic of REs.
The concept of 'repeat zero or more times' is a bit strange outside of
the domain of REs because repetition and '0 times' don't go well
together - normally.
Maybe it helps if you think of '*' as
_optionally_ repeat <whatever> as many times as you like

Back to your expression
For example if we have ra*t this will match rat, raat, raaaaaat but no rt or
radfgt
Sure it will match rt:
- you ask for an 'r' - you get it
- you ask for 'zero or more a(s)' - you get zero a(s) -> ok
- you ask for a 't' - you get it.

But I strongly second Arne's advice: If you want to get seriously
involved with REs get some material and study it.
HTH
Helmut Giese
 

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