Home » Category » Java Essentials

Java Essentials: yet another REGEX question

300| Thu, 15 Nov 2007 03:16:00 GMT| jloyd01a| Comments (9)

I am having problems with a wild card file search routine I am writing. The basic idea is that I can search a file system for strings like "test.*" or "*.xml" or "test*.xml" (and so on). I use * as my wild card character.

My problem test case is when the code encounters "*.*" which would be any file with an extension. The pattern I have come up with is .*(\.) Which works in an external file search tool I have (Agent Ransack) but gets me nothing in Java.

If you want to see my code I would be glad to post it. If anyone knows of code that will do the same thing let me know.

Message was edited by:

jloyd01

By the way. I looked at all the API, tutorials and searched through the other regex posts, but did not find an answer.

Keywords & Tags: regex, java, essentials

URL: http://java.itags.org/java-essentials/80964/
 
«« Prev - Next »» 9 helpful answers below.
Should it be more like ".*(\.).*" Am I on the right track or do I need to take another look a regular expressions?

jloyd01a | Thu, 12 Jul 2007 08:19:00 GMT |

.* will match any file.*\..* will match any file with a dot in the name.Kaj

kajbja | Thu, 12 Jul 2007 08:19:00 GMT |

Works great thanks for the help, I gave you some duke dollars

jloyd01a | Thu, 12 Jul 2007 08:19:00 GMT |

> Works great thanks for the help, I gave you some duke> dollarsThanks

kajbja | Thu, 12 Jul 2007 08:19:00 GMT |

Hi,You probably need to post some code. Not much, just the part that tries to list files, and which gives an empty result for .*Kaj

kajbja | Thu, 12 Jul 2007 08:19:00 GMT |

Here is some code. This is the first time I have tried to write such a function, I'm sure the is a better way. I still think regular expressions will somehow play a part.

Notes on code:

- escapeRegex(String) is a function I wrote to escape meta characters in the input String.

Here is the part of the program dealing with the regex:

final Pattern pattern = Pattern.compile(patternBuffer.toString());

FilenameFilter filter = new FilenameFilter()

{

public boolean accept(File dir, String name)

{

if (pattern.matcher(name).matches())

{

return true;

}

return false;

}

};

searchResults = dir.listFiles(filter);

here is the entire function:

public static File[] wildcardFileSearch(File dir, String searchString, String wildcard)

{

File[] searchResults = null;

String beginString = null;

String endString = null;

ArrayList middleParts = new ArrayList();

boolean wildcardFirst = false, wildcardLast = false;

//check input

if (dir == null || !dir.exists() || !dir.isDirectory())

{

throw new IllegalArgumentException("Argument dir must be a directory that exists");

}

if (StringUtils.isEmpty(searchString) || StringUtils.isEmpty(wildcard))

{

throw new IllegalArgumentException("Arguments searchString and wildcard must be non-empty, non-null Strings");

}

//if there are no wild cards, do a basic search

if (searchString.indexOf(wildcard) == -1)

{

return fileSearch(dir, searchString);

}

//check special conditions: wildcard first or last char

if (searchString.startsWith(wildcard))

wildcardFirst = true;

if (searchString.endsWith(wildcard))

wildcardLast = true;

//divide file into pieces based on the wildcard

String[] searchParts = searchString.split(escapeRegex(wildcard));

int i = 0;

int stop = searchParts.length;

if (!wildcardFirst)

{

beginString = searchParts[0];

i = 1;

}

if (!wildcardLast)

{

endString = searchParts[searchParts.length - 1];

stop = searchParts.length - 1;

}

for (; i < stop; i++)

{

middleParts.add(searchParts[i]);

}

//complile patern

StringBuffer patternBuffer = new StringBuffer();

if (beginString != null)

{

patternBuffer.append("^(");

patternBuffer.append(escapeRegex(beginString));

patternBuffer.append(").");

patternBuffer.append("*");

}

else

{

patternBuffer.append(".*");

}

if (middleParts.size() > 0)

{

Iterator iter = middleParts.iterator();

while (iter.hasNext())

{

String element = (String) iter.next();

if (StringUtils.isEmpty(element))

continue;

patternBuffer.append("(");

patternBuffer.append(escapeRegex(element));

patternBuffer.append(")");

}

}

if (endString != null)

{

patternBuffer.append(".*");

patternBuffer.append("(");

patternBuffer.append(escapeRegex(endString));

patternBuffer.append(")$");

}

final Pattern pattern = Pattern.compile(patternBuffer.toString());

FilenameFilter filter = new FilenameFilter()

{

public boolean accept(File dir, String name)

{

if (pattern.matcher(name).matches())

{

return true;

}

return false;

}

};

searchResults = dir.listFiles(filter);

return searchResults;

}

jloyd01a | Thu, 12 Jul 2007 08:19:00 GMT |

> final Pattern pattern = Pattern.compile(patternBuffer.toString());Print patternBuffer just before that line. What does it print?Kaj

kajbja | Thu, 12 Jul 2007 08:19:00 GMT |

For the input:dir = File(c:\\data)searchString = "*.*"wildcard = "*"I get ".*(\.)"BTW = JRE/JDK Version = 1.4.2_14. I cannot upgrade (I blame IBM)

jloyd01a | Thu, 12 Jul 2007 08:19:00 GMT |

> I get ".*(\.)"That expression isn't correct. That will only match strings that ends with a dot.Kaj

kajbja | Thu, 12 Jul 2007 08:19:00 GMT |

Java Essentials Hot Answers

Java Essentials New questions

Java Essentials Related Categories