Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Regular Expression To Catch Duplicates

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Regular Expression To Catch Duplicates

    I want to know how to use "matches()" to catch when a duplicate appears in my String "xmlString" (which contains an xml file).

    So for example the following fragment should return "false":

    <Countries>
    <Country>BE</Country>
    <Country>DE</Country>
    <Country>GB</Country>
    </Countries>

    Whereas the following fragment should return "true":

    <Countries>
    <Country>BE</Country>
    <Country>DE</Country>
    <Country>GB</Country>
    <Country>DE</Country>
    </Countries>

    So far my comparison looks like this:

    boolean containsDuplicateCountry = xmlString.matches(".*<Country>[A-Z]{2}</Country>.*");

    which just returns "true" for any upper case 2 digit territory code. I have no idea how to amend this to check for a duplicate. Any ideas would be really appreciated. Thanks,

    Daniel


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Regular Expression To Catch Duplicates

    Parse and capture each value using a Pattern/Matcher and use a a Set to check for duplicates.
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
    The Set Interface (The Java™ Tutorials > Collections > Interfaces)

  3. The Following User Says Thank You to copeg For This Useful Post:

    daniel_el (February 9th, 2012)

Similar Threads

  1. Replies: 3
    Last Post: September 30th, 2011, 08:45 AM
  2. How to remove the regular expression from a string ?
    By srimanta in forum Java Theory & Questions
    Replies: 5
    Last Post: July 20th, 2011, 03:07 PM
  3. Regular Expression help
    By medoos in forum Java SE APIs
    Replies: 0
    Last Post: March 19th, 2011, 07:23 PM
  4. Simple Hashsets and regular expression problem !
    By burningflower in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 10th, 2011, 01:43 PM
  5. Using Regular Expression (regex) in Java Programming
    By lordelf007 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 14th, 2010, 10:29 AM

Tags for this Thread