Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading Regex Pattern From Properties File

807569Sep 21 2006 — edited Sep 21 2006
In my effort to make my application very customizable, I have an XML properties file with the key
<entry key="filePattern">\\d{4}-\\d{2}-\\d{2}.txt$</entry>
That is read into a properties object using the "loadFromXML" command. The value is used to create a Pattern object, and then I am using a Matcher object to search for the match to the pattern within a string that holds the name of file.

Here is the code that reads in the property:
			String userDir = System.getProperty("user.dir");
			fis = new FileInputStream( userDir + "/" +  collectorConfigFile );
			prop.loadFromXML(fis);
That works fine. Here is the code the reads in the regex pattern:
patLogFileName = Pattern.compile( ( prop.getProperty("filePattern") ).trim() ); 
No problems here, either.

Then it loops through an array of File objects and checks each files name against the pattern:
matLogFileName = patLogFileName.matcher( children.getName());
if( matLogFileName.find()) {
myLogger.debug( children[i].getName() + " matches the log file naming pattern. - '" + ( prop.getProperty("filePattern") ).trim() + "'" );

File tmpFile = new File( children[i].getAbsolutePath() );
if ( tmpFile.lastModified() >= fromDate.getTimeInMillis() && ( tmpFile.lastModified() <= toDate.getTimeInMillis() ) ) {
myLogger.debug( children[i].getName() + " is in the eligible date range." );
tmpCal.setTimeInMillis( tmpFile.lastModified() );
myLogger.debug("Added file " + tmpFile.getAbsolutePath() + " with creation time " + String.format("Duke's Birthday: %1$tm %1$te,%1$tY", tmpCal ));
fileList.add( children[i]) ;
}
else {
myLogger.debug( children[i].getName() + " is not in the eligible date range." );
}


The files are failing to match, even when they do match the pattern. Here is a sample output line from the debug code:
 
DEBUG - starstb1_2006-09-01.txt does not match the log file name pattern. - \\d{4}-\\d{2}-\\d{2}.txt$'
However, if I hardcode the regex value like so:
patLogFileName = Pattern.compile( "\\d{4}-\\d{2}-\\d{2}.txt$" );
Then it matches the way it is supposed to:
DEBUG - Added file \\mer2-sysloggp3\syslog\starstb1\2006\Sep\starstb1_2006-09-01.txt with creation time Duke's Birthday: 09 2,2006
I can't figure out why it works when it is hard-coded, but not when it is read in from the properties file.

Any advice?

Thank You,

John A Gooch
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 19 2006
Added on Sep 21 2006
3 comments
3,484 views