Results 1 to 7 of 7
  1. #1
    rrotstain is offline Member
    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    Default advanced regular expressions

    Hi folks,

    Excuse me if this question has already been asked, i searched but could not find it.

    What i need to do is use regular expressions to parse text that contains a location, such as "Las Vegas, Nevada, USA". However, I need the regular expression to look like "<city>, <state>, <country>" since there can be many values for cities, states, and countries. This is a textbook example for the java.util.regex.Pattern and java.util.regex.Matcher classes except for the fact I don't want to write a regular expression such as "Las Vegas|San Francisco|New York|.....". There are many cities, states, and countries in the world. I need to be able to customize it such as whenever the Pattern class sees the text "<city>", it should match against a particular list of valid values.

    This may be solvable with a framework like ANTLR, but I'm wondering whether it can be done using plain Java.

    Regards,
    Ronen

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For regular expressions to work, the data must be regular, must have a definable pattern. Do you know of any patterns that define your data and are never broken?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    For regular expressions to work, the data must be regular
    <useless Saturday nitpick>
    Any finit amount of data over a finit alphabet is regular.
    </useless Saturday nitpick>

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    <useless Saturday nitpick>
    Any finit amount of data over a finit alphabet is regular.
    </useless Saturday nitpick>

    kindest regards,
    <Useless reply to useless nitpick>
    Perhaps I should say that regex may not be the best tool to use for parsing text that is not defined by a regular grammar.

    Fire back when ready.
    </Useless reply to useless nitpick>

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    <Useless reply to useless nitpick>
    Perhaps I should say that regex may not be the best tool to use for parsing text that is not defined by a regular grammar.

    Fire back when ready.
    </Useless reply to useless nitpick>
    <corollary to previous useless Saturday nitpick>
    Every finit amount of data can be described by a finit regular expression.
    </corollary to previous useless Saturday nitpick>

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    <corollary to previous useless Saturday nitpick>
    Every finit amount of data can be described by a finit regular expression.
    </corollary to previous useless Saturday nitpick>
    <Eristic Rebuttal>
    And yer nuthin' but a Grolsch-swilling defenestratin' fool.
    </Eristic Rebuttal>

    I win.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    <Eristic Rebuttal>
    And yer nuthin' but a Grolsch-swilling defenestratin' fool.
    </Eristic Rebuttal>

    I win.
    <very intelligent reply>
    Your mother was a hamster and your father smelt like elderberries.
    </very intelligent reply>

    <corollary to very intelligent reply>
    :-P :-P :-P
    </corollary to very intelligent reply>

    So there!

    kindest regards,

    Jos (<-- loves intellectual discussions ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Regular Expressions Help
    By Death Sickle in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 04:21 AM
  2. Regular Expressions problem
    By mohammedelbes in forum Advanced Java
    Replies: 7
    Last Post: 01-05-2011, 11:12 AM
  3. regular expressions
    By sozeee in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 09:58 PM
  4. Regular Expressions in java
    By blue404 in forum Advanced Java
    Replies: 2
    Last Post: 09-26-2008, 03:43 AM
  5. Regular expressions quantifiers
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-25-2007, 11:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •