SnO2d
asked on
Regular Expression
table name = 'dbo.EXCHANGE', constraint name blah blah DELETE FROM dbo.EXCHANGE WHERE exchangeCode blah blah table name = 'dbo.EXCHANGE', constraint name = blah blah
I am trying to create a regular expression which will only find the word EXCHANGE once in the above string,
specifically in the '...DELETE FROM dbo.EXCHANGE WHERE...'
The regular expression I am trying to create is:
String regex = "[dbo]{1}.([A-Z]+?).[WHERE ]{1}";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
However when I pring out what is found by the matcher, I get:
EX
EX
EX
while (matcher.find()) {
System.out.print(matcher.s tart(1) + "-" + matcher.end(1) + ": ");
System.out.println(matcher .group(1)) ;
}
Can someone tell me the regex I need to use. I need to include the flag 'Pattern.DOTALL' when compiling the pattern.
There can also be any number of underscores (including none) in the table name, for example:
EXCHANGE
EXCHANGE_HISTORY
EXCHANGE_HISTORY_TEMP
etc
Thank you
I am trying to create a regular expression which will only find the word EXCHANGE once in the above string,
specifically in the '...DELETE FROM dbo.EXCHANGE WHERE...'
The regular expression I am trying to create is:
String regex = "[dbo]{1}.([A-Z]+?).[WHERE
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
However when I pring out what is found by the matcher, I get:
EX
EX
EX
while (matcher.find()) {
System.out.print(matcher.s
System.out.println(matcher
}
Can someone tell me the regex I need to use. I need to include the flag 'Pattern.DOTALL' when compiling the pattern.
There can also be any number of underscores (including none) in the table name, for example:
EXCHANGE
EXCHANGE_HISTORY
EXCHANGE_HISTORY_TEMP
etc
Thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm running some DELETE sql but it is failing because of a constraint.
I therefore want to try to report something sensible back to the screen, not the stacktrace.
That works a treat, thanks
I therefore want to try to report something sensible back to the screen, not the stacktrace.
That works a treat, thanks
ASKER
What about underscores in the table name?
For example,
EXCHANGE
EXCHANGE_HISTORY
EXCHANGE_HISTORY_TEMP
For example,
EXCHANGE
EXCHANGE_HISTORY
EXCHANGE_HISTORY_TEMP
ASKER
Got it ...
String regex = "[dbo]{1}.([A-Z]+?[_?[A-Z] *?]*?)\\W+ [WHERE]{1} ";
String regex = "[dbo]{1}.([A-Z]+?[_?[A-Z]
Isn't using regexes a bit over the top for such simple substringing functionality? You could use something like
public static String truncateStatement(String statement, String tableName) {
String sl = statement.toLowerCase();
int i = 0;
if ((i = sl.indexOf(tableName, i)) > -1) {
return (i = sl.indexOf(tableName, i + 1)) > -1? statement.substring(0, i).trim() : statement;
}
else {
return statement;
}
}
public static String truncateStatement(String statement, String tableName) {
String sl = statement.toLowerCase();
int i = 0;
if ((i = sl.indexOf(tableName, i)) > -1) {
return (i = sl.indexOf(tableName, i + 1)) > -1? statement.substring(0, i).trim() : statement;
}
else {
return statement;
}
}
ASKER
I don't know the table name in advance. I only know it will appear between The strings 'dbo.' and ' WHERE'
oh ok
What is your intention here?