[英]How to extract words from a given string in Java
我正在嘗試提取所有單詞(甚至旁邊有括號的單詞-編程語言中的方法/函數)
但是我只能得到第一個單詞,而不是所有單詞。 如何遍歷與給定regex
匹配的所有單詞?
這是我嘗試過的。 我的String
是我正在閱讀的文本文件,它看起來像這樣。
infile >> name;
infile >> Id;
cout << name << " " << Id << endl;
hwp = compute_hw_participation (infile);
tests = compute_tests(tests, infile);
totalscore = compute_totalscore (totalscore, infile);
printRecord (name, Id, hwp, tests, totalscore, outfile);
infile >> name;
return 0;
}
此外,我試圖找到methods
在這個String
的方法是
compute_hw_participation(infile)
compute_totalscore(totalscore,infile)
printRecord(名稱,Id,hwp,測試,總成績,輸出文件) //此方法在方法名稱和括號之間有一個空格,盡管有空格,我也需要獲取括號(直到括號的末尾),如何我也實現了嗎?
這是我嘗試過的:
package com.codeingrams.recursion;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author Jananath Banuka
*/
public class Test {
private static final Pattern p = Pattern.compile(" [^\\s(]+\\([^)]*\\)|\\S+");
public static void main(String[] args) {
String text = "\n"
+ "compute_hw_participation(infile) infile >> name; \n"
+ "while(!infile.eof())\n"
+ "{\n"
+ "infile >> Id;\n"
+ "cout << name << \" \" << Id << endl;\n"
+ "hwp = compute_hw_participation (infile);\n"
+ "tests = compute_tests(tests, infile);\n"
+ "totalscore = compute_totalscore (totalscore, infile);\n"
+ "// grade\n"
+ "printRecord (name, Id, hwp, tests, totalscore, outfile);\n"
+ "infile >> name; \n"
+ "}\n"
+ "\n"
+ "return 0;\n"
+ "}\n"
+ "";
// create matcher for pattern p and given string
Matcher m = p.matcher(text);
// if an occurrence if a pattern was found in a given string...
if (m.find()) {
// ...then you can use group() methods.
System.out.println(m.group(0)); // gives only infile
System.out.println(m.group(1)); //this gives error arrayIndexoutofBound
}
}
}
輸出:
compute_hw_participation(infile)錯誤:線程“ main”中的異常java.lang.IndexOutOfBoundsException:com.codeingrams.recursion.Test.main(測試中,java.util.regex.Matcher.group(Matcher.java:538)處沒有組1 .java:44)
您需要一個與函數調用匹配的模式,即名稱,可能的空格,左括號,一些參數和右括號。
查看Javadoc for Pattern,您會看到可以在正則表達式中使用的字符類。 你需要:
\\w
\\s*
, *
表示零到很多次 \\(
[^)]*
[
和]
創建一個組, ^
是負號,表示該組中什么都沒有。 \\)
然后,您需要為每個反斜杠添加另一個反斜杠,因為Java字符串還將反斜杠用於特殊字符,例如\\n
。
您還需要添加括號以捕獲您感興趣的數據。這也是您必須對括號進行引用以使其匹配的原因-不帶括號的括號表示分組或捕獲。
這樣,總正則表達式為(\\w+\\s*\\([^)]*\\))
。
這是完整的程序:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String text = "\n"
+ "compute_hw_participation(infile) infile >> name; \n"
+ "while(!infile.eof())\n"
+ "{\n"
+ "infile >> Id;\n"
+ "cout << name << \" \" << Id << endl;\n"
+ "hwp = compute_hw_participation (infile);\n"
+ "tests = compute_tests(tests, infile);\n"
+ "totalscore = compute_totalscore (totalscore, infile);\n"
+ "// grade\n"
+ "printRecord (name, Id, hwp, tests, totalscore, outfile);\n"
+ "infile >> name; \n"
+ "}\n"
+ "\n"
+ "return 0;\n"
+ "}\n";
Pattern p = Pattern.compile("(\\w+\\s*\\([^)]*\\))");
Matcher m = p.matcher(text);
while (m.find()) {
System.out.println(m.group());
}
}
}
您會很快看到這種簡單方法的局限性:它還認為while(!infile.eo()
是一個函數,因為它看起來像一個函數調用。該代碼不知道任何可能的語言關鍵字。不會在while
表達式中捕獲最后一個結束括號,這是因為它不計算括號,只是在第一個結束括號處停止,正則表達式也不了解注釋或字符串,並且會很高興地選擇注釋掉的代碼或字符串就像"foo()"
。
因此,對於要處理的語言,使用真正的解析器幾乎總是更好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.