• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regexes  RSS feed

 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey All,

Is it possible for me to create a regex which indicates something along these lines:
Pattern.compile("marsu[pial]");
where Pattern matches only "marsup", "marsupi", "marsupia", and "marsupial"?

I had originally thought it was, and that was the proper syntax, but then I realized that the above regex also matches "marsul", or any of the other bracketed letters or combination thereof.

After doing some research the best I can come up with is:
Pattern.compile("(marsup)|(marsupi)|marsupia)|(marsupial)");
But that's not really working for me efficency-wise, nor does it seem much better than simply creating String[] and running String.equals.

Thank you.
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, "[pial]" matches exactly one of the letters 'p', 'i', 'a', or 'l' each time it's applied. The order in which they're listed has no effect on the order in which they're matched. Here's a regex that will optionally match on or more of those letters, but only if they're in the correct order:
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't know too much about regexes, but what about

marsu[p][pi][pia][pial]?
 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first regex works, though I added $ so that the user couldn't correctly enter something like marsuphorsecowpie and still get a match.

The second one appears to only respond to 'marsupial.' However, if changed to:
marsu[p]?[pi]?[pia]?[pial]?$
It becomes a lot more functional, but has the issue of matching strings like following:
marsuppipia
marsupipia
marsupiapial

Another option which seems to work identically to the first regex is:
marsu(p|(pi)|(pia)|(pial))?$

EDIT: Oh. And thank you both for responding.

[ December 18, 2008: Message edited by: Alex Birmingham ]
[ December 18, 2008: Message edited by: Alex Birmingham ]
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats funny, yea I don't doubt that mine was wrong, regexes are my next area of indepth study... I didn't even know you could use () in them... (G) or conditional statements for that matter. However, I must say that knowing about conditionals is going to make the matter less daunting.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Useful section about them in the Java� Tutorials.
 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hate to keep this thread alive behind it's natural life, but what about...
a regex represents an article, an adjective, and a noun, and matches either just the (partial or whole) noun, or (partial or whole) adjective + (partial or whole) noun, or article + adjective + noun (all partial or whole).
Here's what I've got after reading the abovementioned tutorial:
^(a.?)?(g(r(e(y.?)?)?)?)?d(o(o(r)?)?)?$

It's 99 percent there, however it doesn't match:
'a gr door'
'a gre door'
or any other string in which grey is partial.

Any hints as to why not? Other parts of speech match partially.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm..... This is a case where it may be good to generate the regex dynamically, as a program that needs something like this, probably needs it for more than one string pattern.

How about generating the regex like so? ...



This will dynamically generate "marsup(i(a(l)?)?)?" for the regex.

Henry
[ December 19, 2008: Message edited by: Henry Wong ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!