regular expression

J

Johnathan Smith

hey

im lookiing to be able to write a regular expressions which deals with
10 letter strings which end in either "ed" or "ing"

any help would be much appreciated

thanks
 
R

Robert Klemme

2008/1/14 said:
im lookiing to be able to write a regular expressions which deals with
10 letter strings which end in either "ed" or "ing"

any help would be much appreciated

Doesn't sound too difficult. What did you came up with so far?

Kind regards

robert
 
J

Johnathan Smith

hey

only a very basic start im afraid

^([a-zA-Z]{10})$

i.e
ten letter word (upper or lower case)
basically i need to get it now so it only deals with words with the
suffix's a mentioned

any pointers?

thanks
 
R

Robert Klemme

2008/1/14 said:
hey

only a very basic start im afraid

^([a-zA-Z]{10})$

i.e
ten letter word (upper or lower case)
basically i need to get it now so it only deals with words with the
suffix's a mentioned

any pointers?

You can do either

if /^\w{7}(?:\wed|ing)$/i =~ str
# process match
end

or

if str.length == 10 && /(?:ed|ing)$/i =~ str
# process match
end

There are of course more alternatives... :)

Kind regards

robert
 
J

Johnathan Smith

i appreciate the help

however, im still having problems

im using the regex coach to see if i can get a match but im being
unsuccessful

im using

^\w{10}(?:\ed|ing)$

with the target string controlled

yet not getting a match
can you see why?

thanks
 
R

Robert Klemme

2008/1/14 said:
i appreciate the help

however, im still having problems

im using the regex coach to see if i can get a match but im being
unsuccessful

im using

^\w{10}(?:\ed|ing)$

with the target string controlled

yet not getting a match
can you see why?

Yes.

robert
 
J

Johnathan Smith

thank you.

would be be able to offer any suggestions as to where im going wrong?

thanks
 
R

Rick DeNatale

i appreciate the help

however, im still having problems

im using the regex coach to see if i can get a match but im being
unsuccessful

im using

^\w{10}(?:\ed|ing)$

with the target string controlled

yet not getting a match
can you see why?

That's looking for 10 word characters FOLLOWED by either ed or ing.
 
J

Johnathan Smith

thank you.

what ive basically got now is

\w{7}(ed|ing)

was basically works for ing but only matches a 9 letter word for ed

there must be a better way to do it
ive been playing around to with no luck

any suggestions would be greatly appreciated

thanks
 
M

Moises Trovo

The "\w{7}" matches with 7 chars, so to match all your expression it
have to have 9 chars ending with ed and 10 chars ending with ing.
 
R

Robert Klemme

2008/1/14 said:
thank you.

what ive basically got now is

\w{7}(ed|ing)

was basically works for ing but only matches a 9 letter word for ed

there must be a better way to do it
ive been playing around to with no luck

Maybe you should rather *read* what people write if they take the time
to help you.
any suggestions would be greatly appreciated

Have been made already.

Cheers

robert
 
P

Phrogz

thank you.

would be be able to offer any suggestions as to where im going wrong?

You didn't copy/paste exactly what Robert wrote. You're missing a
piece. Hint: \e is not meaningful in a (Ruby) regexp.
 
P

Paul Stickney

You can also expand the alternation all the way through.
For instance, imagine: (?:\w{X}ed|\w{Y}ing)
for suitable numeric values of X and Y and applied anchoring.
 
R

Robert Klemme

2008/1/15 said:
You can also expand the alternation all the way through.
For instance, imagine: (?:\w{X}ed|\w{Y}ing)
for suitable numeric values of X and Y and applied anchoring.

But this is less efficient with NFA regexp engines because it has to
do more backtracking. Actually, I believe it is most efficient to
have the longest trailing portion first, e.g. /^\w{7}(?:ing|\wed)$/i.
If you are interested you can play a bit with
http://www.weitz.de/regex-coach/.

Kind regards

robert
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top