Results 1 to 6 of 6
  1. #1
    jhoang is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default How to search for specific pattern in string?

    I need to search for a specific pattern in a string. I need to identify if a string contains a 10 character sequence that begins with C, and contains both and only numbers and letters (i.e. no symbols), and all characters must be uppercase. I did look into regex's but I didn't fully understand them, and I am not even sure if this is what to use for such a task. I would also need to get the index of this character sequence when found as well. Can anyone help me out?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to search for specific pattern in string?

    Quote Originally Posted by jhoang View Post
    I need to search for a specific pattern in a string. I need to identify if a string contains a 10 character sequence that begins with C, and contains both and only numbers and letters (i.e. no symbols), and all characters must be uppercase. I did look into regex's but I didn't fully understand them, and I am not even sure if this is what to use for such a task. I would also need to get the index of this character sequence when found as well. Can anyone help me out?
    Regular expressions are made for this: the following RE does the job: C[0-9A-Z]{9} see the API documentation of the Pattern class for the details.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jhoang is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: How to search for specific pattern in string?

    Thanks Jos! got it working!

  4. #4
    jhoang is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Regex (must contain a digit)

    Right now, this regex C[0-9A-Z]{9}\\s matches with something that begins with C contains only 0-9 and A-Z and ends with a space. How do i add a feature in this regex to also ensure the match will contain atleast 1 digit (0-9)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Regex (must contain a digit)

    PM received:
    Quote Originally Posted by jhoang
    Hi Darryl. this regex matches what I need C[0-9A-Z]{9}\\s but i need it to limit its matches to something that MUST include atleast one digit as well. How can i add this into my regex?
    Please keep technical questions in the forum where the discussion can benefit the community. Also, it is better to post follow-up questions in the same thread. I'm merging the two threads.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Regex (must contain a digit)

    Quote Originally Posted by jhoang View Post
    Right now, this regex C[0-9A-Z]{9}\\s matches with something that begins with C contains only 0-9 and A-Z and ends with a space. How do i add a feature in this regex to also ensure the match will contain atleast 1 digit (0-9)
    You've already been pointed to the Pattern API. Here are a couple more learning resources:
    Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 11
    Last Post: 08-15-2011, 10:12 AM
  2. How to withdraw a specific from a String?
    By equal in forum New To Java
    Replies: 6
    Last Post: 05-29-2011, 09:02 PM
  3. Specific syntax? java.util.regex.Pattern$Node
    By lmhelp2 in forum New To Java
    Replies: 1
    Last Post: 04-09-2010, 11:32 AM
  4. How to Search for a specific element in a MultiGraph?
    By JavaInLove in forum Advanced Java
    Replies: 2
    Last Post: 10-05-2008, 03:19 PM
  5. Replies: 0
    Last Post: 11-20-2007, 04:59 PM

Tags for this Thread

Posting Permissions

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