简体   繁体   English

如何在Java正则表达式中获得美元符号

[英]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 我一直在通过以下方法查找: 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). (<>只是在我解析Twitter时示意句子推文的开始)。 I want to extract FB TWTR. 我想提取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: 这是一种1-regex方法( 请参阅demo ),仅在对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]+) . 这里使用的正则表达式是(?:<|(?!^)\\G)[^>]*?\\$([AZ]+)

It matches: 它匹配:

  • (?:<|(?!^)\\G) - A literal < and then at the end of each successful match (?:<|(?!^)\\G) -文字< ,然后在每次成功匹配的末尾
  • [^>]*? - 0 or more characters other than > (as few as possible) - >以外的0个或多个字符(尽可能少)
  • \\$ - literal $ \\$ -文字$
  • ([AZ]+) - match and capture uppercase letters (replace with what best suits your purpose, perhaps \\\\w ). ([AZ]+) -匹配并捕获大写字母(用最适合您的目的的字母替换,也许\\\\w )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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