retrieveing substring using regular expression? - regex

My friend they are like ummm file name which has specific format should be written with like first year (YY) then month (MM) then that sequential number (SEQ) and stored in database and I have to check that all files are exist with same year and same month so...
I'm using regex to catch theses parts to check that concept... and thanks for your help
I'm using the where clause to filter records then I use substr to catch these part I told you about so the query will be something like that..
Select substr(column_name1,4,4),substr(column_name1,0,2),substr(column_name1,2,2),
into seqvar,yearvar,dayvar
from table1
where regex_like (FILENAME,'(CDR-)([1-9][0-9]{0,3})(_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{4}_UK1\.FCDR)
is it best way and fastest to do something like that ??
or using like clause insted of regex at where cluase are better ??
please all your opinion with that case

If you want only the part/group you have mentioned , use this :
`/\((.*?)\)/`
try this in your firebug
var str = 'CDR-([1-9][0-9]{0,3})_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{4}_UK1\.FCDR';
var res = /\((.*?)\)/.exec(str);
console.log(res[1]);
will output : [1-9][0-9]{0,3}
if you want output like ([1-9][0-9]{0,3})
use : /(\(.*?\))/

Since oracle doesn't support lookaheads, maybe it will be easyer to use REGEXP_REPLACE
May look like this:
select regexp_replace(FILENAME, '(CDR-)([1-9][0-9]{0,3})(_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{4}_UK1\.FCDR)', '\2')
from ....

Related

How to use contains and replace in power query together?

I am trying to add a step in power query to replace any value "?" with UNKNOWN, but I need to add some sort of conditional or contains statement, and have not been able to figure out how to make it work.
Here is a sample of what the city values look like
and if I use something like this:
= Table.ReplaceValue(#"Uppercased Text","?","UNKNOWN",Replacer.ReplaceText,{"City"})
it will give results like
but I just want it to displace "UNKNOWN" once. I have been trying to combine the below with an if statement or contains, but not been able to make it work correctly.
If statement like this should work:
#"Replace" = Table.ReplaceValue(#"Uppercased Text", each [City], each if Text.Contains([City], "?") then "UNKNOWN" else [City], Replacer.ReplaceValue, {"City"})

RPA(blueprism) date validation

I am trying to validate date which is coming from an excel sheet , the format should be in dd/mm/yyyy
i tried with regex pattern [0-9]{2}/[0-9]{2}/[0-9]{4}
but this won't work with single digit date and we since we cannot add 0 at start in excel sheet this pattern ain't working. (this is for blueprism tool which have a action for regex matching]
To build in the resiliency you require, you'll have to accept either 1 or 2 digits for both dd and mm:
[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}
Since you are mentioning that you are working with BluePrism, are you sure that you really need Regex for validating dates? Because BP has built-in feature for that callable directly inside a Calc stage - checkout following example (you can see Expression of selected Calc stage in top Expression bar).
The function used for validating dates is IsDate([Some date as string]), result is saved into a Flag data-item.
After the check you can use that Flag data-item in a Decision block and do whatever you consider appropriate if a date is not an actual date.
Note: of course, if you are working with lists/datatables in a Code stage instead of iterating over a collection in process layout, then you need something else, but this might be still helpful.
In Code stage I would probably simply use DateTime.Parse(String) method which is able to automatically convert a date in a form of a String into a DateTime object instance; example:
' DateTime.Parse throws an Exception if parsing failed.
Dim valid As Boolean = False
Try
Dim d = DateTime.Parse(First_Date)
valid = True
Catch e As Exception
valid = False
End Try
See more about parsing dates using DateTime.Parse at MSDN: https://msdn.microsoft.com/en-us/library/1k1skd40(v=vs.110).aspx
There is also a nice post about parsing dates here: https://stackoverflow.com/a/18465222/7439802
In blue prism you can use
FormatDate(Now(), FormatOfDate)
For comparing two dates, initially Convert(" FormatDate ") in same format and then you can compare.
For " FormatDate " option you can refer Help of Blue prism and search for dateadd --> select the Calculation and decision

PL/SQL regexp_like filters

I want to delete some tables and wrote this procedure:
set serveroutput on
declare
type namearray is table of varchar2(50);
total integer;
name namearray;
begin
--select statement here ..., please see below
total :=name.count;
dbms_output_line(total);
for i in 1 .. total loop
dbms_output.put_line(name(i));
-- execute immediate 'drop table ' || name(i) || ' purge';
End loop;
end;
/
The idea is to drop all tables with table name having pattern like this:
ERROR_REPORT[2 digit][3 Capital characters][10 digits]
example: ERROR_REPORT16MAY2014122748
However, I am not able to come up with the correct regexp. Below are my select statements and results:
select table_name bulk collect into name from user_tables where regexp_like(table_name, '^ERROR_REPORT[0-9{2}A-Z{3}0-9{10}]');
The results included all the table names I needed plus ERROR_REPORT311AUG20111111111. This should not be showing up in the result.
The follow select statement showed the same result, which meant the A-Z{3} had no effect on the regexp.
select table_name bulk collect into name from user_tables where regexp_like(table_name, '^ERROR_REPORT[0-9{2}0-9{10}]');
My question is what would be the correct regexp, and what's wrong with mine?
Thanks,
Alex
Correct regex is
'^ERROR_REPORT[0-9]{2}[A-Z]{3}[0-9]{10}'
I think this regex should work:
^ERROR_REPORT[0-9]{2}[A-Z]{3}[0-9]{10}
However, please check the regex101 link. I've assumed that you need 2 digits after ERROR_REPORT but your example name shows 3.

How to exclude a word from a regular expression in oracle?

I have table (Oracle DB) with some products:
product_1 150
product_2 250
product_1 test 150
product_3
... etc
I want take data only for “product_1” but not for “product_1 test” with only one condition. I use REGEXP_LIKE for it:
SELECT *
FROM TABLENAME
WHERE REGEXP_LIKE(LOWER(PRODUCT), '.*product_1.*(?!test).*')
But it does not work and return empty result. Where is mistake in syntax of regexp?
Oracle does not support lookaheads.
With the products as you show, you can use this:
SELECT * FROM TABLENAME WHERE REGEXP_LIKE(PRODUCT, 'product_\d+(\s*\d+)*', 'c');
This is only based on the product names you have shown. If it does not catch everything you want, give us a better idea of what we are trying to match.
Another option: it's a hack, but if you're confident that "product_digits " should never be followed by a "t", you can use this:
SELECT * FROM TABLENAME WHERE REGEXP_LIKE(PRODUCT, 'product_\d+($|\s)($|[^t]).*', 'c');
Only one condition:
SELECT *
FROM TABLENAME
WHERE
REGEXP_INSTR(LOWER(PRODUCT), 'product_1\s')
> REGEXP_INSTR(LOWER(PRODUCT), 'product_1\s+test')

RegEx in the select for DB2

I have a table with one column having a large json object in the format below. The column datatype is VARCHAR
column1
--------
{"key":"value",....}
I'm interested in the first value of the column data
in regex I can do it by .*?:(.*),.* with group(1) giving me the value
How can i use it in the select query
Don't do that, it's bad database design. Shred the keys and values to their own table as columns, or use the XML data type. XML would work fine because you can index the structure well, and you can use XPATH queries on the data. XPATH supports regexp natively.
You can use regular expression with xQuery, you just need to call the function matches from a SQL query or a FLORW query.
This is an example of how to use regular expressions from SQL:
db2 "with val as (
select t.text
from texts t
where xmlcast(xmlquery('fn:matches(\$TEXT,''^[A-Za-z 0-9]*$'')') as integer) = 0
)
select * from val"
For more information:
http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.xml.doc/doc/xqrfnmat.html
http://angocadb2.blogspot.fr/2014/04/regular-expressions-in-db2.html
DB2 doesn't have any built in regex functionality, unfortunately. I did find an article about how to add this with libraries:
http://www.ibm.com/developerworks/data/library/techarticle/0301stolze/0301stolze.html
Without regexes, this operation would be a mess. You could make a function that goes through the string character by character to find the first value. Or, if you will need to do more than this one operation, you could make a procedure that parses the json and throws it into a table of keys/values. Neither one sounds fun, though.
In DB2 for z/OS you will have to pass the variable to XMLQUERY with the PASSING option
db2 "with val as (
select t.text
from texts t
where xmlcast(xmlquery('fn:matches($TEXT,''^[A-Za-z 0-9]*$'')'
PASSING t.text as "TEXT") as integer) = 0
)
select * from val"