• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Regexes

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79704
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic