0

I have a piece of regex I've been using in Perl to parse a large text file containing every stored procedure in an Oracle database. It looks like this:

/create\s+(proc(edure)?|function)\s+\[*(dbo)?\]*\.*\[*(\w+)/i

I'm not too familiar with Perl though, so would ideally like to convert this to Java. I've tried running it through some converters, which produces:

"/create\\s+(proc(edure)?|function)\\s+\\[*(dbo)?\\]*\\.*\\[*(\\w+)/i"

However, this doesn't seem to match any correct input. Can anyone give me some pointers in converting this correctly to Java?

Sample Data:

SET QUOTED_IDENTIFIER ON 
GO
CREATE PROCEDURE AddNewTc                       

@TCN NVARCHAR(100),                
@TCM NTEXT,                
@TCOLE IMAGE = NULL,                
@BYSPID INT = 0,     

Only the "CREATE PROCEDURE" line should be matched.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Andrew Martin
  • 5,619
  • 10
  • 54
  • 92

3 Answers3

1

The converter works just fine, but you will need to change a couple of things.

/create\\s+(proc(edure)?|function)\\s+\\[*(dbo)?\\]*\\.*\\[*(\\w+)/i
  1. Java does not have inline-regexes like you do in perl & javascript. You will need to use the Pattern & Matcher API for regex support.
  2. Due to the lack of inline-regex, pattern modifiers are done separately with flags.

Applying step 1 (remove prepended / and appended /) and step 2 (replace /i with Pattern.CASE_INSENSITIVE) you would get something like:

 Pattern p = Pattern.compile("create\\s+(proc(edure)?|function)\\s+\\[*(dbo)?\\]*\\.*\\[*(\\w+)", Pattern.CASE_INSENSITIVE);
 boolean matches = p.matcher(str).matches();
srbs
  • 634
  • 9
  • 12
0

Try with this one:

"create\\s+((procedure)?|(function))?\\s+\\([(dbo)?\\]\\.\\[)?(\\w+)"

It should work, but you need to add upper case if you need to match some, or add a "i" flag to your regex to ignore case.

in Java, ecape character \ must be escaped with another \ to escape in regex syntax (too much escape !)

DEMO

Supamiu
  • 8,501
  • 7
  • 42
  • 76
0

The JRegex project (which I am neither affiliated with nor have been a user of) might interest you. It claims to provide a Java library to handle perl regular expressions.

collapsar
  • 17,010
  • 4
  • 35
  • 61