Tuesday, November 24, 2009

Using regular expressions in Java

Below is an example of using Java's regular expression functionality to replace the spaces inside of a parenthesis with a underscore:

String inputStr = "1 (2 3) 4 (5 6) 7 8 9";
String thePatternStr = "\\([\\w\\d\\s]*\\)";
Pattern pattern = Pattern.compile(thePatternStr);
Matcher matcher = pattern.matcher(inputStr);
StringBuffer sb = new StringBuffer();
while(matcher.find())
{
String match = matcher.group();
String matchWithUnderscore = match.replace(' ', '_');
matcher.appendReplacement(sb, matchWithUnderscore);
}
matcher.appendTail(sb);
System.out.println("Input string: " + inputStr);
System.out.println("Modified string: " + sb.toString());

This should give the following output:

Input string: 1 (2 3) 4 (5 6) 7 8 9
Modified string: 1 (2_3) 4 (5_6) 7 8 9

Using regular expressions in Java

Below is an example of using Java's regular expression functionality to replace the spaces inside of a parenthesis with a underscore:

String inputStr = "1 (2 3) 4 (5 6) 7 8 9";
String thePatternStr = "\\([\\w\\d\\s]*\\)";
Pattern pattern = Pattern.compile(thePatternStr);
Matcher matcher = pattern.matcher(inputStr);
StringBuffer sb = new StringBuffer();
while(matcher.find())
{
String match = matcher.group();
String matchWithUnderscore = match.replace(' ', '_');
matcher.appendReplacement(sb, matchWithUnderscore);
}
matcher.appendTail(sb);
System.out.println("Input string: " + inputStr);
System.out.println("Modified string: " + sb.toString());

This should give the following output:

Input string: 1 (2 3) 4 (5 6) 7 8 9
Modified string: 1 (2_3) 4 (5_6) 7 8 9

Labels:

Thursday, May 08, 2008

Determine files that are modified

find . -mtime -1 -print

Wednesday, September 19, 2007

Using Awk to rename multiple files

The following awk script will rename files named like:

_TEST.ABC.123.XML.1231123

to the following

TEST.ABC.123.XML


for f in `ls _TEST*`
do
newfile=`echo $f | grep -o TEST.[A-Z]*.[0-9]*.XML`
mv $f $newfile
done

Monday, August 20, 2007

Utility for determining which files a program is using

The Process Explorer for Windows is a utility for determining which files an application is currently using. This can come in handy especially when you are trying to delete a file but cannot because some process is still using it. I highly recommend it. Below is the URL:


http://www.microsoft.com/technet/sysinternals/Utilities/ProcessExplorer.mspx

Friday, August 17, 2007

JVM parameters to use a debugger for Java

Below are the JVM arguements that can be used to allow a debugger to connect to your Java application

-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=port

Monday, October 23, 2006

Drop all tables for Microsoft SQL Server

Resource: John's Blog

WHILE EXISTS(SELECT [name] FROM sys.tables WHERE [type] = 'U')
BEGIN
DECLARE @table_name varchar(50)
DECLARE table_cursor CURSOR FOR SELECT [name] FROM sys.tables WHERE [type] = 'U'
OPEN table_cursor
FETCH NEXT FROM table_cursor INTO @table_name
WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
EXEC ('DROP TABLE [' + @table_name + ']')
PRINT 'Dropped Table ' + @table_name
END TRY
BEGIN CATCH END CATCH
FETCH NEXT FROM table_cursor INTO @table_name
END
CLOSE table_cursor
DEALLOCATE table_cursor
END

Tuesday, September 26, 2006

Encrypting file on Unix

crypt: Encodes (encrypts) or decodes a file
usage: crypt [options][file]
options: password
example: crypt key <> encrypted.file
example: crypt key < encrypted.file | lpr

Resource:

http://www.u.arizona.edu/udocs/unix-cmds.html#crypt