【问题标题】:Remove all the numeric and alphanumeric characters from text file using使用从文本文件中删除所有数字和字母数字字符
【发布时间】:2014-08-16 06:38:02
【问题描述】:

我有 2 个文本文件:

File1 - 这个文件的格式是user_id tweet_id tweet_text

文件 1

60730027    6298443824  thank you echo park. you've changed A LOT, but as long as I'm getting paid to make you move, I'm still with it! 2009-12-03 02:54:10
60730027    6297282530  fat Albert Einstein goin in right now over here!!!  2009-12-03 01:35:22

文件2
这个文件的格式是genome_id name ascii_name

4045417 Southwest Indent    Southwest Indent
4045418 Southeast Point     Southeast Point     

下面是sn-p读取文件1的代码:

public void readfromFile() throws FileNotFoundException {
    Scanner inputStream;
    String source=null;
     FileInputStream file = new FileInputStream("file1.txt");   
        String regex = "/[a-zA-Z ]+/";
        Scanner fileScan = new Scanner(file); 

        while(fileScan.hasNextLine()){
            word = fileScan.nextLine();
            word = word.replaceAll(regex, "").toLowerCase();
            PrintWriter outputStreamName = new PrintWriter(new FileOutputStream("temp.txt"));
            outputStreamName.printf("%s",word);
}

我的意图首先是用空值替换 user_id、tweet_id、genome_id 中存在的数据。然后将大写值转换为小写。但是,现在每当此代码处理 file1 时,文本文件都不会发生任何变化。我也想知道发生了什么。当我将它输出到控制台时,我得到了输出。

预期输出:

thank you echo park youve changed a lot but as long as im getting paid to make you move im still with it

fat albert einstein goin in right now over here

【问题讨论】:

  • 如果你想改变你应该写的文件,使用 PrintWriter 打开文件并写入你的数据!
  • 这个I'myou've改成Imyouve怎么样?被接受了吗?
  • @user3218114 - 我的错误。我已经从输出中删除了这些值。
  • @WajdyEssam - 我在循环中添加了以下代码。 PrintWriter outputStreamName = new PrintWriter(new FileOutputStream("temp.txt")); outputStreamName.printf("%s",word);
  • 别担心,看看我的回答,如果你想要别的东西,请告诉我。

标签: java regex string replaceall


【解决方案1】:

根据预期输出,您想要替换除字母、点和单词之间的空格以外的所有内容。

[^a-zA-Z. ]+|(?<=\d)\s*(?=\d)|(?<=\D)\s*(?=\d)|(?<=\d)\s*(?=\D)

这里是online demo

或者尝试不使用Lookaround

[^a-zA-Z. ]+|\d\s+\d|\D\s+\d|\d\s+\D

这里\s匹配任何空白字符[\r\n\t\f ]

示例代码:

String regex = "[^a-zA-Z. ]+|(?<=\\d)\\s*(?=\\d)|(?<=\\D)\\s*(?=\\d)|(?<=\\d)\\s*(?=\\D)";
str.replaceAll(regex,"");

输出:

thank you echo park. youve changed A LOT but as long as Im getting paid to make you move Im still with it
fat Albert Einstein goin in right now over here

要从输出中排除',请使用[^a-zA-Z.' ]+,否则I'myou've 将更改为Imyouve

更好使用[a-zA-Z']+ 仅获取所有单词。这里是demo

示例代码:

String str = "60730027    6297282530  fat Albert Einstein goin in right now over here!!!  2009-12-03 01:35:22 ";
Pattern p = Pattern.compile("[a-zA-Z']+");
Matcher m = p.matcher(str);
while (m.find()) {
    System.out.print(m.group()+" ");
}

输出:

fat Albert Einstein goin in right now over here 

注意:因此您正在检查下一行

变化:

source = inputStream.next();

收件人:

source = inputStream.nextLine();

【讨论】:

  • 是的,我明白你的意思。我对代码进行了一些更改,并编辑了我的帖子。你能检查一下吗?我想将大写值更改为小写并将源中存在的值转储到文本文件中。
  • 只需使用m.group().toLowerCase()并写入文件即可。
【解决方案2】:
public void readfromFile() throws Exception
{

    FileInputStream file = new FileInputStream("file1.txt");    

    StringBuilder builder = new StringBuilder();
    int ch;
    while((ch = file.read()) != -1){
        builder.append((char)ch);
    }

    System.out.println(builder.toString().replaceAll("[^a-zA-Z\\s]", ""));

}

扫描仪过滤空字符串。

前任

Scanner scanner = new Scanner("60730027    6298443824  thank");
while(scanner.hasNext())    //Read from file till the last line of the file.
{
    System.out.print(scanner.next());
}

输出

607300276298443824谢谢

所以我们不能使用扫描仪。

【讨论】:

  • 您的代码 sn-p 也不起作用。该文件完好无损,没有任何更改。
【解决方案3】:

试试这个

s = s.replaceAll("\\d+\\s+\\d+\\s+", "").replaceAll(" +\\S+ \\S+$", "");

【讨论】:

    猜你喜欢
    • 2021-02-25
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2012-11-15
    相关资源
    最近更新 更多