I have a string/column something like this
String a = \"000003023_AggregateStopLossLimit_W x3A 973911_2012-12-22.PDF\";
I want to create a subs
replaceAll
takes a regex as an argument, if the substring contains regex markers (such as [
, +
for example) you will get an unexpected behaviour.
You can use replace
instead which does the same thing but takes a string as a parameter.
Apart from that, if you know that you will have a space and a _
as delimiters, AND the substring in between does not occur elsewhere, then your approach looks fine. You could possibly make it slightly more readable with intermediate variables:
int start = a.indexOf(" ");
int end = a.indexOf("_", start);
String b = a.substring(0, start) + a.substring(end, a.length());
final String r = a.replaceAll(" .*?(?=_)", "");
if you print the r, it gave output:
000003023_AggregateStopLossLimit_W_2012-12-22.PDF
You should replace REGEX_REPLACE function.
http://docs.oracle.com/cd/B12037_01/server.101/b10759/functions115.htm#SQLRF06302
Apart from Regex issues in the code you provided, i found it less readable also.
Try following:
int f = a.indexOf(" ");
int l = a.lastIndexOf("_");
a = a.substring(0,f+1) + a.substring(l+1, a.length);
The Java solution given by @Kent above is very elegant and I recommend it. That said, if you want to accomplish this using Oracle's regex engine, you might try the following:
WITH t1 AS (
SELECT '000003023_AggregateStopLossLimit_W x3A 973911_2012-12-22.PDF' AS filename
FROM dual
)
SELECT filename, REGEXP_REPLACE(filename, ' [^_]*', '')
FROM t1
To do it in the database:
select regexp_replace ( your_string
, '([^[:space]]*) (.*)_(.*)'
, '\1_\3') as new_string
from your_table
Unfortunately Oracle doesn't have any syntax to enforce laziness (non-greediness) in its regex implementation. That's why my original '(.*) ' included the x3A
: it matched up to the last space with a following underscore. However, the negation syntax will isolate the string up to the first space.
"The '_' after W is missing. Any chance to get that also?"
You can format the replacement string anyway you want. The easy way out is to do what I have done, and hardcode the underscore between the two matched patterns. Alternatively you could make it a search pattern in its own right and include it in the replacement string (although you're more more likley to do that for more complicated searches).
Oracle introduced Regular Expressions in 10g; the functions are covered in the documentation. The regex implementation is POSIX compliant, so it lacks some of the functions you might have come across in say Perl. The Regex support is detailed in an appendix to the SQL ref.
As for tutorials, well I have a much-thumbed copy of the O'Reilly pocket book; I was given my copy at Open World 2003 but the ebook is reasonably priced. Buy it here. Anotgher good starting point is a series of threads by cd
on the OTN forum: start reading here.