Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find string which doesn't have a certain string.

Status
Not open for further replies.

kanghao

IS-IT--Management
Jul 4, 2004
68
KR
I want to find lines which doesn't have "filter me out".

I don't want to use indexOf method of String class, I want regular expressions.

 
I mean regular expression in java equivalent to the "-v" option in grep command in UNIX.

Thanks.
 
I can assure you that the indexOf() method will be quicker than regex - is there any particular reason why you don't want to use indexOf() ?

--------------------------------------------------
Free Database Connection Pooling Software
 
Nothing special.
Here is my code example.
and I want to use a regExs array.
and I want to know the reg ex which accomplish the not-to-have-string pattern.

package test.log;

import java.io.*;

public class AnalyzeSysLog {
public static void main(String[] args) throws FileNotFoundException, IOException {

String[] regExs = {
"NullPointerException"
, "[^\t]"
, "\\[[\\d]{4}"
, "\\[APPLNO="
// , "N+o+r+m+a+l"
};

String [] string_not_to_have = {
""
, ""
};

File file_syslog = new File("c:/test/20050321sys.log");
File file_res = new File(file_syslog.getAbsolutePath()+".res.txt");
LineNumberReader lnr = new LineNumberReader(new FileReader(file_syslog));

StringBuffer sb = new StringBuffer();
String line_read = "";
while((line_read=lnr.readLine())!=null){
for (int i=0; i<regExs.length; i++) {
if(debug.RegEx.indexOf(line_read,regExs)==-1){
break;
}else if(i+1 == regExs.length){
sb.append(lnr.getLineNumber() + "\t" + line_read + "\n");
}
}
}
FileOutputStream fos = new FileOutputStream(file_res);
byte[] b = sb.toString().getBytes();
fos.write(b);
fos.close();
}
}
 
It's awkward:
Code:
String string = "doesn't contain it";
String string2 = "contains filter me out";
String regexp = "(?:.(?!filter me out))+";

if ( string.matches( regexp ) )
   System.out.println( "String doesn't contain 'filter me out'" );

if ( string2.matches( regexp ) )
   System.out.println( "String2 doesn't contain 'filter me out'" );
 
Great. It really works.

To me the above is somewhat strange.
Can you explain it in detail for me?
Thanks.
 
Is the below possible with one regular expression pattern?

true
if a line which contains the string "start" at the beginning and
contains the string "must_string" in any place of the line and
contains the string "another_must_string" in any place of the line and
does not contain "not" at the end of the line
false otherwise.
 
To explain the first pattern.

(?: - begin a group, without capturing
. - any character
(?!filter me out) - not followed by the string `filter me out'
) - close the group
+ - one or more of the group

i.e. the string must consist of one or more of any character that isn't followed by that string.

For your last request - this might not work as it's untested (I don't have a regexp interpreter on this machine I'm on right now). This is really ugly and won't scale to more must_strings. Should work though

Code:
String regexp = "start.*?(?:must_string.*?another_must_string|another_must_string.*?must_string).*?end";
 
Thanks so much.
by the way, what do you mean capturing?

>(?: - begin a group, without capturing
 
In a regexp, when you `capture' something, it means that it's saved in memory and can be accessed after the match is performed (see the group() methods in the java.util.regex.Matcher class). In this case, we don't need to be able to use the matched string. We only want to group those together, so we use (?:...) instead of (...)
 
Thanks.

Your solution,
String regexp = "(?:.(?!filter me out))+";
doesn't filter out the String "filter me out bla..."
so I modified to
String regexp = "(?:.*(?!filter me out))+";

it doesn't work.

why?
 
Try this instead:
Code:
String regexp = "(?:(?!filter me out).?)+";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top