How to fix this regexp? - regex

I have a regexp (,\s*?\n)(\s*?)) and according to https://regex101.com/ it should work. The only problem is that it isn't. What I want to achieve is:
'some text,
)'
will get converted to
'some text
)'
I know that if that regexp of mine would somehow work than the output string would be:
'some text)'
Is there any way to not move the ')' to the same line as 'some text'?
The sample that I used for testing:
declare
l_example varchar2(32000);
begin
l_example :='some text,
)';
dbms_output.put_line(l_example);
l_example := regexp_replace(l_example, '(,\s*?\n)(\s*?\))', '\2');
dbms_output.new_line;
dbms_output.put_line(l_example);
END;
/

Please check this:
regexp_replace(l_example, '(,\s*?'|| CHR(10)||' *)(\s*?\))', '\2', 1, 1,'m');
db<>fiddle

Related

Replace pair of % in oracle

please, I have in Oracle table this texts (as 2 records)
"Sample text with replace parameter %1%"
"You reached 90% of your limit"
I need replace %1% with specific text from input parameter in Oracle Function. In fact, I can have more than just one replace parameters. I have also record with "Replace this %12% with real value"
This functionality I have programmed:
IF poc > 0 THEN
FOR i in 1 .. poc LOOP
p := get_param(mString => mbody);
mbody := replace(mbody,
'%' || p || '%', parameters(to_number(p, '99')));
END LOOP;
END IF;
But in this case I have problem with text number 2. This functionality trying replace "90%" also and I then I get this error:
ORA-06502: PL/SQL: numeric or value error: NULL index table key value
It's a possible to avoid try replace "90%"? Many thanks for advice.
Best regards
PS: Oracle version: 10g (OCI Version: 10.2)
Regular expressions can work here. Try the following and build them into your script.
SELECT REGEXP_REPLACE( 'Sample text with replace parameter %1%',
'\%[0-9]+\%',
'db_size' )
FROM DUAL
and
SELECT REGEXP_REPLACE( 'Sample text with replace parameter 1%',
'\%[0-9]+\%',
'db_size' )
FROM DUAL
The pattern is pretty simple; look for patterns where a '%' is followed by 1 or more numbers followed by a '%'.
The only issue here will be if you have more than one replacement to make in each string and each replacement is different. In that case you will need to loop round the string each time replacing the next parameter. To do this add the position and occurrence parameters to REGEXP_REPLACE after the replacement string, e.g.
REGEXP_REPLACE( 'Sample text with replace parameter %88888888888%','\%[0-9]+\%','db_size',0,1 )
You are getting the error because at parameters(to_number(p, '99')). Can you please check the value of p?
Also, if the p=90 then then REPLACE will not try to replace "90%". It will replace "%90%". How have you been sure that it's trying to replace "90%"?

Match a file name that includes the path and a period within the name

I have a file I need to take just its name:
/var/www/foo/dog.tur-tles.chickens.txt
I want to match just the:
dog.tur-tles.chickens
I have tried this in regexer:
([^\/]*)$
This matches:
dog.tur-tles.chickens.txt
I can't figure out how to only exclude that last period.
You can assume it will always be a .txt, but I wanted to build in the ability that if a file was named dog-turtles.txt.txt it would see that the name is dog-turtles.txt.
You could use something like so: ([^\/]*)(\.).+?$.
An example is available here. Not though that this will fail for extensions such as .tar.gz and so on.
You may use File::Basename.fileparse to get the file name, then use rindex to get the last index of . and then get the required substring using substr:
use File::Basename;
$x = fileparse('/var/www/foo/dog.tur-tles.chickens.txt');
print substr($x, 0, rindex($x, '.')) . "\n";
Output of a sample program:
dog.tur-tles.chickens
$name = ($pathname =~ s{.*/}{}r =~ s{\.[^.]+$}{}r)
substitution 1 : just remove dir
substitution 2 : just remove extension if presente
Just add .txt to your regex and since * is greedy by default it will match everything till last .txt
([^\/]*)\.txt$
Input:
/var/www/foo/dog.tur-tles.chickens.txt.txt
/var/www/foo/dog.tur-tles.chickens.txt
Output:
dog.tur-tles.chickens.txt
dog.tur-tles.chickens
See DEMO

Delete comments in a String?

I want to remove simple // comments in a string.
My String is called input
def input = '''test //kommentar
|
|//noch einer
|
|und noch //einer'''.stripMargin()
The regex is \s*\/\/.*$ and can be tested here http://regexr.com?37ks0
In my code i have input = input.replaceAll(/\s*\/\/.*$/ , '')
But it doesn't work. Can anybody help me ?
At the very least, you need to make sure that the $ anchor is allowed to match the end of each line, not just the end of the entire string:
input = input.replaceAll(/(?m)\s*\/\/.*$/ , '')
But what if // occur, say, in a quoted string? Or in any other circumstance where they do not mean "start of comment"?
And if you want to keep the //noch einer line as a blank line in your output, you could try:
input.replaceAll( '(?m)//.*$' , '' )
Of course if the line above was in your input text, then all of this regex munging would break the input code, as that line would become input.replaceAll('(?m)
As a general rule, this sort of regular expression parsing of code is never a good idea
Ok i got the answer from a college.
If it was one line, the code in the description works.
Because I have multilines, I have to use the following:
input = input.replaceAll(Pattern.compile(/\s*\/\/.*$/, Pattern.MULTILINE), '')

How can I use a PL/SQLregular expression to replace an HTML tag and its contents with a like number of '?s'?

In Pl/SQL i need to replace something like;
'MOUSE RAT <FONT COLOR="#FF0000">DOG</FONT> CAT ELEPHANT'
with
'MOUSE RAT ????????????????????????????????? CAT ELEPHANT'
Basically I need to replace an HTML tag and everything in between with a placeholder of '?' equal to the same length as the string I am replacing. The good news is the tag will always be a font tag.
Will a REGEXP_REPLACE do this?
IF so what does the pattern look like?
REGEXP_REPLACE() replaces a pattern, so whilst it's useful for finding what you want to replace you can't replace the removed string with something of the same length.
The following will replace the HTML:
regexp_replace(str, '</?FONT.*>')
You then need to add in question marks to the length of the removed string, i.e. the length of the string prior to removal minus the length of the string now.
I'm not really certain there's a good way of going about this unfortunately. You'll have to use a character to notify you that this is where the question marks need to be once the string has been replaced. Something like the following would work:
replace( regexp_replace(str, '</?FONT.*>', '?')
, '?'
, lpad( '?'
, length(str) - length(regexp_replace(str, '</?FONT.*>', '?')) - 1
, '?'
)
)
I really don't like it though... If the entire thing is HTML it would be easier and better to use a proper parser and then you could just replace all the data in the one node.
Although I like PL/SQL, I would not recommend that. PL/SQL mighty tool for data manipulation but is not too handy for parsing. This is example where Java stored procedure can be more efficient. Especially when you will have to refactor your code several times.
Also REGEX_REPLACE works with VARCHARs(max size 32KB) while you maybe need work with CLOBs.
I wrote a function that is easier to understand than Ben's code, but probably less efficient, and certainly less elegant. I haven't decided which to use, what do you think?
FUNCTION REPLACE_WITH_PLACEHOLDER(IN_STRING IN VARCHAR2, START_STRING IN VARCHAR2, END_STRING IN VARCHAR2, PLACEHOLDER IN VARCHAR2) RETURN VARCHAR2
IS
OUT_STRING VARCHAR2(32767);
START_POSITION BINARY_INTEGER := 0;
END_POSITION BINARY_INTEGER;
SEARCH_LENGTH BINARY_INTEGER;
SEARCH_STRING VARCHAR2(500);
REPLACE_STRING VARCHAR2(500);
BEGIN
OUT_STRING := IN_STRING;
START_POSITION := INSTR(OUT_STRING,START_STRING);
WHILE START_POSITION > 1
LOOP
END_POSITION := INSTR(OUT_STRING,END_STRING,START_POSITION) + LENGTH(END_STRING);
IF END_POSITION > 0
THEN
SEARCH_LENGTH := (END_POSITION - START_POSITION);
SEARCH_STRING := SUBSTR(OUT_STRING,START_POSITION,SEARCH_LENGTH);
REPLACE_STRING := LPAD(PLACEHOLDER,SEARCH_LENGTH,PLACEHOLDER);
OUT_STRING := REPLACE(OUT_STRING,SEARCH_STRING,REPLACE_STRING);
ELSE
EXIT;
END IF;
START_POSITION := INSTR(OUT_STRING,START_STRING);
END LOOP;
RETURN OUT_STRING;
END REPLACE_WITH_PLACEHOLDER;

AS3 split() command

Is there a way I can split by more than one character? I do not mean a combination of characters, but an array of specific choices. For example:
s = "john is tall,sue is small";
s.split(" ");
trace(s);
The output in this circumstance would be:
'john' 'is' 'tall,sue' 'is' 'small'
However, what if I wanted to edit out the comma as well such that the output was:
'john' 'is' 'tall' 'sue' 'is' 'small'
How can I do this? I'm pretty sure it's done with regex, but I'm a little lost.
Thank you in advance!
AS3's split() method accepts a regular-expression as input, so you should be able to use the following:
var str:String = "john is tall,sue is small";
var re:RegExp = /[, ]/;
var results:Array = str.split(re);
You simply need a regular expression that will match on ',' or ' ' characters. Very simply it is:
/[, ]/g