Regular Expressions

Discussion in 'Java' started by jack.smith.sam@gmail.com, Oct 7, 2006.

  1. Guest

    Hi All,

    I have a string which can be in these two formats : "<b>word" or "word"
    (<b> may or may not be present)
    I used the following regular expression to extract word (word is
    similar to email ID and may containt letter,-,_):

    [<b>]*([\\w-_]+)

    but for some words like "best' it outputs "est" and removes the "b". I
    think [<b>] means < or b or >.
    Can you help me solving this issue(preferably without using additional
    parentheses)?
    Thanks a lot.
     
    , Oct 7, 2006
    #1
    1. Advertising

  2. Hi Jack,

    On Sat, 07 Oct 2006 05:54:17 -0700, jack.smith.sam wrote:
    > I have a string which can be in these two formats : "<b>word" or "word"
    > (<b> may or may not be present)
    > I used the following regular expression to extract word (word is
    > similar to email ID and may containt letter,-,_):
    >
    > [<b>]*([\\w-_]+)

    Try this: "(?:<b>)?([\\w-_]+)"


    > but for some words like "best' it outputs "est" and removes the "b". I
    > think [<b>] means < or b or >.

    The problem is, that the [] parenthesis treats the characters not as
    string, but as individual characters which can appear individially.


    Greetings,
    Markus
     
    Pielmeier Markus, Oct 7, 2006
    #2
    1. Advertising

  3. wrote in news:1160225657.635244.130570
    @m7g2000cwm.googlegroups.com:

    > I have a string which can be in these two formats : "<b>word" or "word"
    > (<b> may or may not be present)
    > I used the following regular expression to extract word (word is
    > similar to email ID and may containt letter,-,_):
    >
    > [<b>]*([\\w-_]+)
    >
    > but for some words like "best' it outputs "est" and removes the "b". I
    > think [<b>] means < or b or >.
    > Can you help me solving this issue(preferably without using additional
    > parentheses)?


    Another, perhaps simpler, alternative is of course to solve it with indexOf
    and substring. It takes a bit more space in your code, though.

    String extractedString = null;
    if (myString.indexOf("<b>") == 0)
    extractedString = myString.substring(3);
    else
    extractedString = myString;

    --
    Martin Gerner
     
    Martin Gerner, Nov 7, 2006
    #3
  4. Lew Guest

    Martin Gerner wrote:
    > Another, perhaps simpler, alternative is of course to solve it with indexOf
    > and substring. It takes a bit more space in your code, though.
    >
    > String extractedString = null;
    > if (myString.indexOf("<b>") == 0)
    > extractedString = myString.substring(3);
    > else
    > extractedString = myString;


    Why the "throwaway" initialization of extractedString = null?

    - Lew
     
    Lew, Nov 12, 2006
    #4
  5. Lars Enderin Guest

    Lew skrev:
    > Martin Gerner wrote:
    >> Another, perhaps simpler, alternative is of course to solve it with
    >> indexOf and substring. It takes a bit more space in your code, though.
    >> String extractedString = null;
    >> if (myString.indexOf("<b>") == 0)
    >> extractedString = myString.substring(3);
    >> else
    >> extractedString = myString;

    >
    > Why the "throwaway" initialization of extractedString = null?


    I prefer

    String extractedString = (myString.indexOf("<b>") == 0 ?
    myString.substring(3) : myString);
     
    Lars Enderin, Nov 12, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas

    Custom Regular Expressions in ASP.net

    Jay Douglas, Nov 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    619
    mikeb
    Nov 3, 2003
  2. mark

    Regular expressions

    mark, Jun 30, 2003, in forum: Perl
    Replies:
    4
    Views:
    1,737
  3. Dustin D.
    Replies:
    1
    Views:
    11,384
  4. Jay Douglas
    Replies:
    0
    Views:
    644
    Jay Douglas
    Aug 15, 2003
  5. Noman Shapiro
    Replies:
    0
    Views:
    262
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page