2

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.

3
  • I think you need to remove the strings which don't have any numbers Commented Aug 20, 2013 at 6:41
  • tell me regular expression to remove the string that don't have any number. Commented Aug 20, 2013 at 6:43
  • I suspect that "2005:0.056 Top Terms: Weight :" might actually be a single line. Commented Aug 20, 2013 at 6:43

4 Answers 4

1

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; e.g.

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0
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!

1 Comment

it's not working because "2005:0.056 Top Terms: Weight " is in same line
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.