\p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ \p{Alpha} An alphabetic character:[\p{Lower}\p{Upper}] \p{Digit} A decimal digit: [0-9]来自 Pattern 的文档
楼主都没明白我的意思。。举个例子吧。比如。 #include<iostream> void main() { int c[20]; int a=5; int b=a+12; for(int i=0;i<10;i++) { c[i]=i; }} 就是说能匹配或者说识别: # include < iostream > void main ( ) { int c [ 20 ] ; int a = 5 ; 依次下去...... 我的意思你们明白了吗??
#include<iostream> int main() { int c[20]; int a=5; int b=a+12; for(int i=0;i<10;i++) { c[i]=i; } return 0; } import java.nio.file.Files; import java.nio.charset.Charset; import java.nio.file.Paths; import java.text.BreakIterator; public class Rocks { public static void main(final java.lang.String[] args) throws java.io.IOException { StringBuilder builder = new StringBuilder(); for (String line : Files.readAllLines(Paths.get("/tmp","rocks.cxx"),Charset.forName("UTF-8"))) { builder.append(line); } String source = builder.toString(); BreakIterator bi = BreakIterator.getWordInstance(); bi.setText(source); for (int start = bi.first(),end = bi.next(); end != BreakIterator.DONE; start = end, end = bi.next()) { System.out.println(source.substring(start,end)); } } } 结果: # include < iostream > int
private void printTable(){ for(String sav:saveTable){ System.out.println(sav); } } }结果:# include < iostream > int main ( ) { int c [ 20 ] ; int a = 5 ; int b = a + 12 ; for ( int i = 0 ; i <= 10 ; i ++ ) { c [ i ] = i ; } return 0 ; }
\p{Alpha} An alphabetic character:[\p{Lower}\p{Upper}]
\p{Digit} A decimal digit: [0-9]来自 Pattern 的文档
#include<iostream>
void main()
{
int c[20];
int a=5;
int b=a+12;
for(int i=0;i<10;i++)
{
c[i]=i;
}}
就是说能匹配或者说识别:
#
include
<
iostream
>
void
main
(
)
{
int
c
[
20
]
;
int
a
=
5
;
依次下去......
我的意思你们明白了吗??
int main()
{
int c[20];
int a=5;
int b=a+12;
for(int i=0;i<10;i++)
{
c[i]=i;
}
return 0;
}
import java.nio.file.Files;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.text.BreakIterator;
public class Rocks {
public static void main(final java.lang.String[] args) throws java.io.IOException {
StringBuilder builder = new StringBuilder();
for (String line : Files.readAllLines(Paths.get("/tmp","rocks.cxx"),Charset.forName("UTF-8"))) {
builder.append(line);
}
String source = builder.toString();
BreakIterator bi = BreakIterator.getWordInstance();
bi.setText(source);
for (int start = bi.first(),end = bi.next();
end != BreakIterator.DONE;
start = end, end = bi.next()) {
System.out.println(source.substring(start,end));
}
}
}
结果:
#
include
<
iostream
>
int
main
(
)
{
int
c
[
20
]
;
int
a
=
5
;
int
b
=
a
+
12
;
for
(
int
i
=
0
;
i
<
10
;
i
+
+
)
{
c
[
i
]
=
i
;
}
return
0
;
}
for(i=0;i<=10;i++)
能输出:
for
(
i
=
0
;
i
<=
10
;
i
++
吗?那个<=要在一起,不能分开,还有那个++。好像上面的程序做不到吧?
String regex = "\\w+|\\p{Punct}";
然后稍作处理。
文件test.txt#include<iostream>
int main()
{
int c[20];
int a=5;
int b=a+12;
for(int i=0;i<=10;i++)
{
c[i]=i;
}
return 0;
}
代码:package com.algorithm;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class SpiltCode {
//你要保留的连续的字符
private String[] codes = {"==","++","--",">=","+=","<="}; //等等..
ArrayList<String> saveTable = new ArrayList<String>(); //保存所有切割出来的串
public static void main(String[] args) {
new SpiltCode().spiltCode();
}
private BufferedReader getBuffreader(String path){
File file = new File(path);
FileInputStream in;
BufferedReader reader=null;
try {
in = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(in));
} catch (Exception e) {
e.printStackTrace();
}
return reader;
}
public void spiltCode(){
BufferedReader reader = getBuffreader("test.txt");
String regex = "\\w+|\\p{Punct}"; //这个可以把单词和一个一个特殊字符全切出来
Pattern p = Pattern.compile(regex);
Matcher m = null;
String line = null;
String pre = ""; //上一个匹配的串
int index = 0;
try {
while((line=reader.readLine())!=null){
m = p.matcher(line);
while(m.find()){
String chars = m.group();
if(isInCodes(pre+chars)){
saveTable.set(--index, pre+chars);
}else{
saveTable.add(chars);
}
pre = chars;
index++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
printTable(); //打印
}
private boolean isInCodes(String code){
for(int i=0;i<codes.length;i++){
if(codes[i].equals(code)){
return true;
}
}
return false;
}
private void printTable(){
for(String sav:saveTable){
System.out.println(sav);
}
}
}结果:#
include
<
iostream
>
int
main
(
)
{
int
c
[
20
]
;
int
a
=
5
;
int
b
=
a
+
12
;
for
(
int
i
=
0
;
i
<=
10
;
i
++
)
{
c
[
i
]
=
i
;
}
return
0
;
}
然后是:
for(int i=0;i<Arry.length;i++)
{
if(Integer.parseInt(Arry[i]))
System.out.printf(Integer.parseInt(Arry[i])));}
为什么这样做不行?有上面好的方法吗,就是必须从数组中的第一个位置找到数组的最后,按照此顺序找出其中的整型数据,因为我要拿他来匹配。
boolean isDigit = true;
ArrayList<Integer> digits = new ArrayList<Integer>();
for(String sav:saveTable){
for(int i=0;i<sav.length();i++){
char c = sav.charAt(i);
if(!Character.isDigit(c)){ //判断每个字符是否是数字
isDigit = false;
break;
}
}
if(isDigit){
digits.add(Integer.parseInt(sav));
System.out.println(sav);
}
isDigit = true;
}
return digits;
}
如果有异常就不是整数
或者用正则