简体   繁体   中英

Java - Filter illegal characters

I'm reading a File in Java and adding all the data to a Map. I'm using JSONParser to parse the result of posting that Map to a URL. Sometimes, it throws:

Exception in thread "Validate26" java.lang.Error: Error: could not match input
at org.json.simple.parser.Yylex.zzScanError(Yylex.java:474)
at org.json.simple.parser.Yylex.yylex(Yylex.java:681)
at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
at com.faris.kingvalidator.ValidateRunnable.run(ValidateRunnable.java:78)
at java.lang.Thread.run(Thread.java:745)

"Validate26" is the name of my thread.

I read up somewhere that this means there's an illegal character somewhere. However, I'm not sure where as I cannot handle this Error, using a try {} and catch {} does not work for this error and the Map<> contains hundreds of strings so printing out every value would take ages to find the certain characters. Instead, I decided that I only want to parse the result, filtering out any characters other than AZ (non-capital too), 0-9, *, _, -, /, comma and period. How may I do this? Thanks in advance.

For Java you have to use \\\\ to insert a single \\

So try \\\\* for all the other chars you wish to escape in regex in java

For the thread exception problem please check this link

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