简体   繁体   中英

How to get a dollar sign in Java regex

I have been lookinig through this : https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

However I still have difficulties to write the right command to get all the expression folllowing this pattern :

 <$FB $TWTR are getting plummetted> 

(<> just signal the beginning of the sentence-tweet actually as I am parsing twitter). I want to extract FB TWTR.

Any help much appreciated.

Here is a 2-step approach: we extract <...> groups with a regex and then split the chunks into words and see if they start with $ .

String s = "<$FB $TWTR are getting plummetted>";
Pattern pattern = Pattern.compile("<([^>]+)>");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
    String[] chks = matcher.group(1).split(" ");
    for (int i = 0; i<chks.length; i++)
    {
        if (chks[i].startsWith("$"))
            System.out.println(chks[i].substring(1));
    }
} 

See demo

And here is a 1-regex approach ( see demo ), use only if you feel confident with regex:

String s = "<$FB $TWTR are getting plummetted>";
Pattern pattern = Pattern.compile("(?:<|(?!^)\\G)[^>]*?\\$([A-Z]+)");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
    System.out.println(matcher.group(1)); 
} 

The regex used here is (?:<|(?!^)\\G)[^>]*?\\$([AZ]+) .

It matches:

  • (?:<|(?!^)\\G) - A literal < and then at the end of each successful match
  • [^>]*? - 0 or more characters other than > (as few as possible)
  • \\$ - literal $
  • ([AZ]+) - match and capture uppercase letters (replace with what best suits your purpose, perhaps \\\\w ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM