I have a txt file in while one line is like this :
"Execution Status: Pass"
I want to take the value out : pass from here.
I am using below code :
String patternString1 = "(Execution) (Status:) (.*)";
Pattern patt1 = Pattern.compile( patternString1 );
BufferedReader r = new BufferedReader(new FileReader( "c:\\abc.txt" ));
String line;
while ( (line = r.readLine()) != null ) {
String g11 = null;
Matcher m1 = patt1.matcher( line );
while ( m1.find() ) {
g11=m1.group(3);
System.out.println(g11+"HI1"); //Line1
}
System.out.println(g11+"HI1"); //Line2
}
While Line 1 is giving the desired output as "pass" I am not getting this expected output from Line 2. Could any one of you please help me in accessing the local variable out of the loop?
Could you try changing following line:
while (m1.find())
to
if(m1.find())
It should give the result you want
How about the easy way:
String result = line.replaceAll(".*: ", "");
This line says "replace everything up to and including colon space with nothing" (ie delete it).
If there is only one instance of this match in single line then use if
instead of while (m1.find())
loop or break
the loop once match is found.
Sample code:
while ( (line = r.readLine()) != null ) {
String g11 = null;
Matcher m1 = patt1.matcher( line );
if( m1.find() ) {
g11=m1.group(3);
}
if(g11 != null){
System.out.println(g11+"HI1"); //Line2
}
}
Declare String g11 as a local variable at the start of your method
String g11=""; -- declare g11 as a local variable
String patternString1 = "(Execution) (Status:) (.*)";
Pattern patt1 = Pattern.compile(patternString1);
Add the rest of your code here.........
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.