This discussion is archived
14 Replies Latest reply: Sep 16, 2009 11:53 AM by 807580 RSS

Regex to find  word starting with $ symbol.

807580 Newbie
Currently Being Moderated
Hi,
I want to find all the words in a JTextPane starting with "$" symbol followed by alphabets or digits.
Below is the code, which I have written
public void matchAllWords(String textPaneData) {
     String regexStr =  "\\b(\\$?(\\w+))\\b";

     Pattern p = Pattern.compile(regexStr);
     Matcher m = p.matcher(textPaneData);

     while (m.find()) {
           System.out.println(m.group());
     }

}
Suppose the text pane data is
hello $world 2$ $test_string $50.0 $5*5

I want the method to match the following words
$world
$test_string
$50.0

But the program is printing:
hello
world
2
test_string
50.0
5*5

Please help me, where am I going wrong.

Thanks in advance.

Regards,
Vaishakh

Edited by: Vaishakh on Sep 16, 2009 4:24 AM
  • 1. Re: Regex to find  word starting with $ symbol.
    807580 Newbie
    Currently Being Moderated
    Not tested, but you could try putting the $ in a character class.
    "[$]"
  • 2. Re: Regex to find  word starting with $ symbol.
    807580 Newbie
    Currently Being Moderated
    Thanks for ur reply, but its not working.
  • 3. Re: Regex to find  word starting with $ symbol.
    791266 Explorer
    Currently Being Moderated
    Vaishakh wrote:
    Thanks for ur reply, but its not working.
    Try
          String regexStr =  "\\s(\\$(\\w+))\\s";
    .. but something is wrong in your description. $50.0 shouldn't be matched according to your description (and it isn't by the regexp in this post)
  • 4. Re: Regex to find  word starting with $ symbol.
    791266 Explorer
    Currently Being Moderated
    Do also note that the regexp doesn't work for the first and last word.
  • 5. Re: Regex to find  word starting with $ symbol.
    807580 Newbie
    Currently Being Moderated
    Thanks, as u said its not working for the first and last word... wat to do?
    I want to match all words starting with '$' symbol followed by alphabets or numbers or underscore. Might be the regex what i have used is only for words.
  • 6. Re: Regex to find  word starting with $ symbol.
    791266 Explorer
    Currently Being Moderated
    Vaishakh wrote:
    Thanks, as u said its not working for the first and last word... wat to do?
    You should first say what it should match and what it shouldn't match. You say that it shoud match alphabets and digits, and then you say that it should match "50.0". Why does it match the dot?
  • 7. Re: Regex to find  word starting with $ symbol.
    807580 Newbie
    Currently Being Moderated
    You should first say what it should match and what it shouldn't match. You say that it shoud match alphabets and digits, and then you say that it should match "50.0". Why does it match the dot?
    Sorry, by numbers, I mean even float and decimal values.
  • 8. Re: Regex to find  word starting with $ symbol.
    791266 Explorer
    Currently Being Moderated
    Vaishakh wrote:
    You should first say what it should match and what it shouldn't match. You say that it shoud match alphabets and digits, and then you say that it should match "50.0". Why does it match the dot?
    Sorry, by numbers, I mean even float and decimal values.
    So what should it do with the values:

    $50,0
    $50.0
    $-50.0
    $50.000.000
    $50,000,000
    $50.0.0
  • 9. Re: Regex to find  word starting with $ symbol.
    807580 Newbie
    Currently Being Moderated
    Thanks, as u said
    http://www.catb.org/~esr/faqs/smart-questions.html#writewell

    How To Ask Questions The Smart Way
    Eric Steven Raymond
    Rick Moen

    Write in clear, grammatical, correctly-spelled language

    We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding (often enough to bet on, anyway). Answering questions for careless and sloppy thinkers is not rewarding; we'd rather spend our time elsewhere.

    So expressing your question clearly and well is important. If you can't be bothered to do that, we can't be bothered to pay attention. Spend the extra effort to polish your language. It doesn't have to be stiff or formal - in fact, hacker culture values informal, slangy and humorous language used with precision. But it has to be precise; there has to be some indication that you're thinking and paying attention.

    Spell, punctuate, and capitalize correctly. Don't confuse "its" with "it's", "loose" with "lose", or "discrete" with "discreet". Don't TYPE IN ALL CAPS; this is read as shouting and considered rude. (All-smalls is only slightly less annoying, as it's difficult to read. Alan Cox can get away with it, but you can't.)

    More generally, if you write like a semi-literate b o o b you will very likely be ignored. So don't use instant-messaging shortcuts. Spelling "you" as "u" makes you look like a semi-literate b o o b to save two entire keystrokes.
  • 10. Re: Regex to find  word starting with $ symbol.
    800285 Newbie
    Currently Being Moderated
    Vaishakh wrote:
    "\\b(\\$?(\\w+))\\b"
    Do you want to use that '?' metacharacter? That's making the '$' optional, which is not what you want..
    kajbj wrote:
    "\\s(\\$(\\w+))\\s";
    The '\s' matches whitespace, so it won't match words at the very beginning or end of the text.

    I don't think regex treats '$' as being part of a word. So because you're using '\b' (word boundary) and because your '$' is optional, it's pretty much ignoring all '$' characters.
  • 11. Re: Regex to find  word starting with $ symbol.
    807580 Newbie
    Currently Being Moderated
    Also if you want $stuff, why do you have a capturing group after the dollar sign?

    It's possible that \b treats dollar signs as indicating a word boundary, and that might be causing results you don't want. I don't know. Check the docs.
  • 12. Re: Regex to find  word starting with $ symbol.
    791266 Explorer
    Currently Being Moderated
    mangst wrote:
    Vaishakh wrote:
    "\\b(\\$?(\\w+))\\b"
    Do you want to use that '?' metacharacter? That's making the '$' optional, which is not what you want..
    kajbj wrote:
    "\\s(\\$(\\w+))\\s";
    The '\s' matches whitespace, so it won't match words at the very beginning or end of the text.

    I don't think regex treats '$' as being part of a word. So because you're using '\b' (word boundary) and because your '$' is optional, it's pretty much ignoring all '$' characters.
    My regexp didn't make it optional. I was about to post something like
    "\\$\\b[a-zA-Z_0-9.]+\\b"
    which is close, but I didn't complete the regexp since I don't know how he wants to treat numbers. The information that he gave isn't complete.
  • 13. Re: Regex to find  word starting with $ symbol.
    807580 Newbie
    Currently Being Moderated
    Thank you very much :)
  • 14. Re: Regex to find  word starting with $ symbol.
    791266 Explorer
    Currently Being Moderated
    Vaishakh wrote:
    Thank you very much :)
    For what? The regexp isn't doing what you wanted it to do?