0

I'm looking for some way to get easily only words and numbers, without any type of weird symbol like ("'/&%$·... so I get from a code:

int i=0;

This: int, i, 0.

Any good function for Java?

alessandrio
  • 4,282
  • 2
  • 29
  • 40

2 Answers2

0

You can't do it directly, you have to get line by line and replace non alphanumerical characters by an empty, then rewrite your line again, to replace all non alphanumerical characters you can use :

line.replaceAll("[^a-zA-Z\\d\\s]", "")

The regex mean, replace all non (^) alphabetic (a-zA-Z) or digit (\d) or space (\s).

You can use something like this :

while ((line = br.readLine()) != null) {//read the line
    putData = line.replaceAll("[^a-zA-Z\\d]", "");//replace all non alphanumerical 
    ....
    bw.write(putData);//write it again
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

You can read file line by line and apply regex on each line. To exclude certain characters ( <, >, %, $, /, \ etc.), you can make a regular expression like this:

[<>%\$=&@]

You can add more in this list. Now,

Pattern p = Pattern.compile("[<>%\$%@]");
Matcher m = p.matcher(unsafeInputString);
if (m.matches())
{
    // Invalid input: reject it, or remove/change the offending characters.
}
else
{
    // Valid input.
}
Kaushal28
  • 5,377
  • 5
  • 41
  • 72