简体   繁体   中英

how to remove specific String from String in java

I am removing specific string from string. First i am reading text file through file reader and after this i am storing file's contents into string array and removing some specific string from string array

my input text is :

    :VL
15
n
3 c

09:0.023
 15th:0.023
 1987:0.025
 1st:0.025
 2:0.013
 2.0:0.043
 2003:0.056
 2005:0.056
    Top Terms: 
    Weight : 
props 
 optional
:  Point:
    1.0:
 15th:0.068

now i am reading this text and storing into string array that is : String [] Result

my code:

for(String t1: Result){
Pattern = Pattern.compile("\\b:[0-9]\\b");
                matcher = pattern.matcher(t1);
                if(matcher.find()){
                    System.out.println(t1);
}

and output i am getting :

09:0.023
 15th:0.023
 1987:0.025
 1st:0.025
 2:0.013
 2.0:0.043
 2003:0.056
 2005:0.056
    Top Terms: 
    Weight : 
 15th:0.068

but i don't want this output. my output should be this :

09:0.023
 15th:0.023
 1987:0.025
 1st:0.025
 2:0.013
 2.0:0.043
 2003:0.056
 2005:0.056
 15th:0.068

Give me some idea about what regular expression i have to apply to get this output.

I suspect that

2005:0.056
    Top Terms: 
    Weight :

is actually a single line ... somehow.

That regex should (only) match lines where you have a "word" consisting of a single digit.


I'm guess that you actually know this (and you "forgot to mention it").

If you want to match these:

 2005:0.056 
 15th:0.023
 1st:0.023
 2nd:0.023
 3rd:0.023

but not these:

 2005:0.056 Top Terms:  Weight :
 1.0:

then you need a stricter regex, and match() rather than find; eg

pattern = Pattern.compile(
              "\\s*[0-9]+(st|nd|rd|th|(\\.[0-9]+))?:[0-9]+\\.[0-9]+\\s*");
for (String t1: Result) {
    matcher = pattern.matcher(t1);
    if (matcher.match()) {
        System.out.println(t1);
    }
}

But at this point I'm guessing what your actual criterion for a "valid" line is.

This may help you.

 BufferedReader br = new BufferedReader(new FileReader("D:\\test.txt"));
    String str = null;
    while ((str = br.readLine()) != null) {
         if((str.contains(":"))){
             String[] arr=str.split(":");
               if(arr.length==2){                      
                       if(Pattern.compile("\\d").matcher(arr[1]).find()){
                           System.out.println(str);
                       }                       
               }
         }
    }

Out put

09:0.023
 15th:0.023
 1987:0.025
 1st:0.025
 2:0.013
 2.0:0.043
 2003:0.056
 2005:0.056
 15th:0.068
for(String t1: Result){
            Pattern p= Pattern.compile("\\b:[0-9]\\b");
                            Matcher m= p.matcher(t1);
                            if(m.find()){
                                System.out.println(t1);
            }

This code works absolutely fine!

i Got this. i am splitting contents after reading from file using regex \\\\s+ after this i am storing data into string array that is String [] Result

and apply same code :

String []result;
String returnValue:
                    file = new FileReader(filename);
            reader = new BufferedReader(file);
            String line = "";
            while ((line = reader.readLine()) != null) {
                returnValue += line + "\n";
            }
            result = returnValue.split("\\s+");
    for(String t1: result){
    Pattern = Pattern.compile("\\b:[0-9]\\b");
                    matcher = pattern.matcher(t1);
                    if(matcher.find()){
                        System.out.println(t1);
    }

and it's giving output same like this:

09:0.023
 15th:0.023
 1987:0.025
 1st:0.025
 2:0.013
 2.0:0.043
 2003:0.056
 2005:0.056
 15th:0.068

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