How to check if a string contains only mixed case? - abap

How could I correct this code line in order to better filter my input conditions :
IF STRING CA SY-ABCDE and STRING CN SY-ABCDE.
My conditions are :
Input must not be all UpperCase or all LowerCase (NAME or name) , 'Name' is acceptable.
Input is acceptable if it contains numbers or symbols (123%Name is acceptable, 123%NAME or 123%name is not).
The code line I provided fulfills the first condition and half of the 2nd, but I can't manage to change it so that it gets denied if input is 123%NAME .

you can also check if there is at least one uppercase letter and one lowercase letter in the string:
IF lv_text CA sy-abcde AND lv_text CA to_lower( sy-abcde ) AND lv_text NA '/\[}$*'.
WRITE:/ 'yes'.
ELSE.
WRITE:/ 'no'.
ENDIF.

You can check string with itself uppercase or lowercase format.
DATA: lv_m TYPE string VALUE 'Abc',
lv_l TYPE string,
lv_u TYPE string.
lv_u = lv_m.
lv_l = lv_m.
TRANSLATE lv_u TO UPPER CASE.
TRANSLATE lv_l TO LOWER CASE.
IF lv_u NE lv_m and lv_u ne lv_m.
MESSAGE 'Not equal' TYPE 'E'.
ENDIF.
Also you can use to_upper / to_lower functions for updated systems.
DATA: lv_m TYPE string VALUE 'Abc'.
IF lv_m ne to_upper( lv_m ) and lv_m ne to_lower( lv_m ).
MESSAGE 'Not equal' TYPE 'E'.
ENDIF.

The idea is to use a negated match() in this case - as it is easier to define what you consider not valid for your requirement.
REPORT ZZ_TEST_MIXED_CASE.
parameters: string type string lower case.
data(lv_match) = match( val = string regex =
`^(\U+|\L+)$|[^\w%]` " <--- Add here after '%' what else you consider as valid 'symbols'
).
if lv_match is initial.
write: / 'OK'.
else.
write: / 'Not OK'.
endif.

Related

how to allow accented charaters in oracle using regexp_replace in oracle?

Case:
To accept person_name satisfying following criteria:
Allows any alphabetic symbols
Space
Dash
Apostrophe
Accent grave
Some pre-calculation has been performed to store the name in the string "PERSON_NAME"
LOGIC: SUBSTR(REGEXP_REPLACE(PERSON_NAME,'[^A-Za-z .`''-]+',''),0,50)
SELECT SUBSTR(REGEXP_REPLACE('cafè','[^A-Z|a-z| |.|`|''|-]+'),0,50)
FROM dual;
Passing almost all cases except in case of accented characters:
For example:
Expected result: cafè [i.e symbol above e ` should not be filtered out]
Actual Result: caf
You can use :
select SUBSTR(REGEXP_REPLACE('cafè-` t *'' {','[[:digit:]]') ,0,50) as "String"
from dual;
String
--------------
cafè-` t *' {
since there's no information about numeric expressions in your restriction list.

regular expression using oracle REGEXP_INSTR

I want to use REGEXP_INSTR function in a query to search for any match for user input but I don't know how to write the regular expression that for example will match any value that includes the word car followed by unspecified numbers of letters/numbers/spaces and then the word Paterson. can any one please help me with writing this regEx?
Ok, so let's break this down.
"any value that includes the word car"
I surmise from this that the word car doesn't need to be at the start of the string, therefore i would start the format string with...
'^.*'
Here the '^' character means the start of the string, the '.' means any character and '*' means 0 or more of the preceding character. So zero or more of any character after the start of the string.
Then the word 'car', so...
'^.*car'
Next up...
"followed by unspecified numbers of letters/numbers/spaces"
I'm guessing that unspecified means zero or more. This is very similar to what we did to identify any characters that might come before 'car'. Where the '.' means any character
'^.*car.*'
However, if unspecified means one or more, then you can use '+' in place of '*'
"then the word Paterson"
I'm going to assume that as this is the end of the description, there are no more characters after 'Paterson'.
'^.*car.*Paterson$'
The '$' symbol means that the 'n' of 'Paterson' must be at the end of the string.
Code example:
select
REGEXP_INSTR('123456car1234ABCDPaterson', '^.*car.*Paterson$') as rgx
from dual
Output
RGX
----------
1

Replace unknown value in SQL query with PostgreSQL

How can I use SQL/PostgreSQL to do something like this (psuedo code):
UPDATE table
SET content = replace(content, 'padding-top: $VAR', 'padding-top: 30px')
WHERE user = 1;
The $VAR is an unknown value, like 80%, 50% or 80px. How could I do this?
You could use regexp_replace:
UPDATE table
SET content = regexp_replace(content, '^padding-top: .*$', 'padding-top: 30px')
WHERE user = 1;
Note that as its name suggests, regexp_replace takes a regular expression. So I am using ^, $ for the beginning and the end, and .* which means "match with any sequence of characters" (. means "match with any character" and * means "match the previous symbol as many times as possible").

Impossible to match a digit with a REGEXP_REPLACE

I try to extract the '930' from 'EM 930' with following Regexp
REGEXP_REPLACE(info,'^[:space:]*[ABCDEFGHIJKLMNOPQRSTUVWXYZ]*[:space:]*([0-9]+)[:space:]*$','\1')
But it returns me the original string.
An idea why ?
Subsidiary question:
Why does the "\1" returned the original string when the pattern is not matched ? I expected it to return NULL, as in my other regexp experiences (eg Perl).
Who I can re-write this in a performant way so that I get of wel the matched string of well NULL ?
Your space character class was not exactly correct. If we change [:space:] to [[:space:]], your regexp_replace works as you expect:
REGEXP_REPLACE(info, '^[[:space:]]*[ABCDEFGHIJKLMNOPQRSTUVWXYZ]*[[:space:]]*([0-9]+)[[:space:]]*$','\1')
For the sake of succinctness, we could use the upper character class, [[:upper:]], for [ABCDEFGHIJKLMNOPQRSTUVWXYZ]. This changes the function invocation to:
regexp_replace(info, '^[[:space:]]*[[:upper:]]*[[:space:]]*([0-9]+)[[:space:]]*$','\1')
Or escape characters could be used in lieu of character classes:
\s space
\w word character
\d digit character
regexp_replace(info, '^\s*\w*\s*(\d+)\s*$','\1')
Explanation:
Since your malformed character class, [:space:], does not match the space that exists between 'EM' and '930', your search by parameter does not match any characters in the source parameter.
Your search by parameter, '^[[:space:]]*[ABCDEFGHIJKLMNOPQRSTUVWXYZ]*[[:space:]]*([0-9]+)[[:space:]]*$', is anchored to the beginning and end of the column, info, thus it can only match the column, info, one time at most.
In your case, there is no match and the character group, '\1' which is associated with '([0-9]*)', has no value.
Consequently, no characters are replaced and you are left with original value of the column, info, 'EM 930'.
interesting variations to better understand this function:
-If your corrected function invocation had no pattern_to_replace_by parameter, '\1', then a NULL would be returned:
regexp_replace(info, '^\s*\w*\s*(\d+)\s*$' ) FROM dual;
-Since you have a pattern_to_replace_by parameter, '\1', and now it has the matching character group, the repeating digit group is returned:
930

Regular expression to validate whether the data contains numeric ( or empty is also valid)

i have to validate the data contains numeric or not and if it is not numeric return 0 and if it is numeric or empty return 1.
Below is my query i tried in SQL.
SELECT dbo.Regex('^[0-9]','123') -- This is returning 1.
SELECT dbo.Regex('^[0-9]','') -- this is not returning 1 but i want to return as 1 and i try to put space in "pattern" also it is not working...
please can any one help....
Thanks in advance
Try:
SELECT dbo.Regex('^[0-9]*$','123')
or better yet:
SELECT dbo.Regex('^\d*$','123')
\d is standard shorthand for [0-9]. Note that when you use [0-9] it means "match exactly one digit. The * wildcard means match zero or more so \d* means match zero or more digits, which includes an empty result.
Your Regex is not quite right: "^[0-9]" checks only, if the first character in the string is a number. Your regex would return 1 on "3abcde".
Use
"^\d*$"
to check if the field contains only numbers (or nothing). If you want to allow whitespace, use
"^\s*\d*\s*$"
If you want to allow signs and decimals, try
"^\s*(+|-)?\d*\.?\d*\s*$"
Try ^[0-9]*$, which will match 0 or more digits only:
SELECT dbo.Regex('^[0-9]*$','')
should return 1, as should...
SELECT dbo.Regex('^[0-9]*$','123')