Results 1 to 8 of 8

Thread: Regex help

  1. #1
    Fabken is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default Regex help

    Dear,

    I have a string with this content = "Dear ?NAME \n Wishing you & the best of everything in ?YEAR !!";
    I would like to replace ?NAME with a value and ?YEAR with a value, those names can change, for example it can be ?FIRSTNAME and ?DATE.
    I was thinking about doing it with String.replace("\\?.*? ", value); to get all words that start with ? and and with a " " but it doesn't work.

    What's wrong?

    Thank you very much!
    .

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Regex help

    Using the String class's indexOf() might be easier.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Fabken is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default Re: Regex help

    Is their a way to capture all words that start with a ? and have all capital letters?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: Regex help

    String.replace does not take a regex. However, String.replaceFirst and String.replaceAll does.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,446
    Rep Power
    18

    Default Re: Regex help

    \?[A-Z]{1,}\b
    Possibly?
    Can't seem to get the word start thing working, but I'm little more than a regex hack.
    So that is ?[one or more caps] word end.

    Increasing the minimum number of caps would make it more solid.
    Figuring out how to prevent it from allowing "something?A", assuming that would be incorrect.

    ETA: Also, re: jims post. You would need something other than String.replace.
    Presumably you want the regex to get the matches, which you'll then do a replace on, using real values from somewhere else.
    Please do not ask for code as refusal often offends.

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,070
    Rep Power
    4

    Default Re: Regex help

    Assuming that the words will only contain ASCII characters.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Fabken is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default Re: Regex help

    Re: Tolls.
    Thank you it worked for me!

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,446
    Rep Power
    18

    Default Re: Regex help

    Quote Originally Posted by gimbal2 View Post
    Assuming that the words will only contain ASCII characters.
    I did say I was a hack!
    :)
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Regex Help Please
    By africanhacker in forum New To Java
    Replies: 7
    Last Post: 07-01-2011, 02:12 AM
  2. How to use Regex?
    By soulmed in forum New To Java
    Replies: 5
    Last Post: 04-28-2011, 07:59 PM
  3. Regex help?
    By l3ane in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 06:04 AM
  4. Using Regex
    By ron87 in forum New To Java
    Replies: 4
    Last Post: 09-12-2010, 06:26 AM
  5. Help with regex
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-29-2010, 12:40 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
  •