0

I am new using java. I wanted to ask, if I have a text file containing different words per line and I want to read that file as a string in order to detect if there are certain words that are written in all caps (abbreviations). The exception being that if the word starts with "#" or and "@" it will ignore counting it. For example I have:

OMG terry is cute #HAWT SMH

The result will be: Abbreviations = 2.

or

terry likes TGIF parties @ANDERSON

The result will be: Abbreviations = 1.

Please help

4

3 回答 3

0

这是您的蜘蛛问题的火箭筒。

(mystring+" ").split("(?<!@|#)[A-Z]{2,}").length-1;
  1. 用空格填充字符串(因为 .split 删除尾随的空字符串)。
  2. 拆分模式“这后面既不是@也不是#,这是两个或多个大写字母”。这将返回不属于缩写的子字符串数组。
  3. 取数组的长度并减去 1。

例子:

mystring = "OMG terry is cute #HAWT SMH";
String[] arr = (mystring+" ").split("(?<!@|#)[A-Z]{2,}").length-1;
//arr is now {"", " terry is cute #HAWT ", " "}, three strings
return arr.length-1; //returns 2
于 2013-11-11T06:15:47.380 回答
0
    String str1 = "OMG terry is cute #HAWT SMH";
    String str2 = "terry likes TGIF parties @ANDERSON";
    Pattern p = Pattern.compile("(?>\\s)([A-Z]+)(?=\\s)");
    Matcher matcher = p.matcher(" "+str1+" ");//pay attention! adding spaces 
                                        // before and after to catch potentials in 
                                        // beginning/end of the sentence
    int i=0;
    while (matcher.find()) {
        i++; //count how many matches were found
    }
    System.out.println("matches: "+i); // prints 2

    matcher = p.matcher(" "+str2+" ");
    i=0;
    while (matcher.find()) {
        i++;
    }
    System.out.println("matches: "+i); // prints 1

输出:

matches: 2
matches: 1
于 2013-11-11T06:02:39.037 回答
0

尝试使用.split(String T)方法和.contains(char C)方法.....我认为它们会对您有很大帮助....

功能拆分:

http://www.tutorialspoint.com/java/java_string_split.htm

函数包含:

http://www.tutorialspoint.com/java/lang/string_contains.htm

于 2013-11-11T05:46:02.357 回答