简体   繁体   中英

Split string help! Splitting a file by certain characters but ignoring strings in quotes in java with regex

for example I have a file like this:

INT f2(INT x, INT y ) 
  BEGIN 
  z := x*x - y*y;
RETURN z; 
END 
INT MAIN f1() 
BEGIN
  INT x;
  READ(x, "A41.input");
  INT y;
  READ(y, "A42.input");
  INT z;
  z := f2(x,y) + f2(y,x);
  WRITE (z, "A4.output");

END

I open the file using:

 FileInputStream fstream = new FileInputStream("A1input.txt");
 DataInputStream in = new DataInputStream(fstream);
 BufferedReader br = new BufferedReader(new InputStreamReader(in));
 String strLine;

and then in order to split each line by characters like ; or = i did this:

String[] SplitString = strLine.split("[\\s\\W]");

I was wondering if there was a way to delete the words in between quotes (eg. "A41.input") using regex so I can ignore those words all together before doing the split

Because it's still open, two possibilities (out of many)

  • "\\b.*?\\b\\W*\\b.*?\\b"
  • or
  • ".*?"

    Escaping backslashes and double quotes in java:

      String str = "READ(y, \\"A42.input\\");"; System.out.println(str); String replacedFirst = str.replaceAll("\\".*?\\"", "\\"\\""); String replacedSecond = str.replaceAll("\\"\\\\b.*?\\\\b\\\\W*\\\\b.*?\\\\b\\"","\\"\\""); System.out.println(replacedFirst); System.out.println(replacedSecond); 
  • 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