loop/logic issues in validating string/character data - openedge

I need to see if the characters at the end of a string are a zip code or zip+4. If there is no zip or zip+4 at the end of this string, then this address is rejected. I don't understand why when I get to the dash character, "-", which is asci 45, my noZip becomes TRUE right here. I am returning a "Yes" from the fnIsDash function, but I leave the loop at that point.
Related to this problem -- I would like to use a regular expression to see if my string is a zip code, it would be "NNNNN-NNNN" or "NNNNN" where N is any digit. But from my research, I didn't see robust regular expression functionally in Progress. Is that true?
FUNCTION fnisNumeric RETURNS LOGICAL (INPUT cCharacter AS CHARACTER) FORWARD.
FUNCTION fnisDash RETURNS LOGICAL (INPUT cCharacter AS CHARACTER) FORWARD.
DEFINE VARIABLE location AS CHARACTER NO-UNDO.
DEFINE VARIABLE zipPlus4Temp AS CHARACTER NO-UNDO.
DEFINE VARIABLE noZip AS LOGICAL NO-UNDO.
DEFINE VARIABLE cThisChar AS CHARACTER NO-UNDO.
DEFINE VARIABLE cTemp AS CHARACTER NO-UNDO.
DEFINE VARIABLE iTemp AS INTEGER NO-UNDO.
location = "124 State Road Mechanicsburg PA 17050-3156".
zipPlus4Temp = SUBSTRING (location, length(location) - 9, 10).
MESSAGE "zipPlus4Temp " + zipPlus4Temp VIEW-AS ALERT-BOX.
noZip = FALSE.
DO iTemp = 1 TO LENGTH(zipPlus4Temp):
IF noZip EQ TRUE THEN LEAVE.
cThisChar = SUBSTRING(zipPlus4Temp,iTemp,1).
MESSAGE STRING(iTemp) + " this char " + cThisChar VIEW-AS ALERT-BOX.
IF iTemp GE 1 AND iTemp GE 5 THEN
IF NOT(fnIsNumeric(cThisChar)) THEN noZip = TRUE.
//noZip becomes true here
IF iTemp EQ 6 THEN
IF NOT(fnIsDash(cThisChar)) THEN noZip = TRUE.
IF iTemp GE 7 AND iTemp GE 10 THEN
IF NOT(fnIsNumeric(cThisChar)) THEN noZip = TRUE.
END.
MESSAGE SUBSTITUTE("zipPlus4Temp &1 is &2",zipPlus4Temp, noZip).
FUNCTION fnIsNumeric RETURNS LOGICAL (i_cc as CHARACTER):
RETURN ASC(i_cc) GE 48 AND ASC(i_cc) LE 57.
END FUNCTION.
FUNCTION fnIsDash RETURNS LOGICAL (i_cc as CHARACTER):
MESSAGE SUBSTITUTE("The character being passed is &1",i_cc) VIEW-AS ALERT-BOX.
MESSAGE "the ascii value of the character being passed is " + STRING(ASC(i_cc)) VIEW-AS ALERT-BOX.
RETURN ASC(i_cc) EQ 45.
END FUNCTION.

Your noZip variable is becoming TRUE before the dash check. You have an error in your digit 1-5 logic. The IF-THEN statement says greater than or equal to 1 and greater than or equal to 5. It should be less than or equal to 5. Change the line to this:
IF iTemp GE 1 AND iTemp LE 5 THEN
Same for the digit 7-10 check, but that isn't affecting the noZip value.
IF iTemp GE 7 AND iTemp LE 10 THEN
And no, Progress doesn't support regular expressions.

Related

Remove quotes if "=" (equal) sign exists in the middle of the string. REGEX

In this string the character “=” differentiates attributes for a product, and commas distinguish variables within an attribute. However, we found that sometimes extra quotes have been added when there are no variables to put together.
The complete string is :
Uso="Protector para patas de silla,mesas,escaleras,muebles","Topes,4-Tipo=Topes,regatones",2-Familia=Ferretería y Plomería,regatones,7-Contenido="12 unidades,4-Origen=China,4-Material=Goma,2-Modelo=Goma transparente,9-Incluye=12 unidades,3-Color=Transparente"
This is right:
Uso="Protector para patas de silla,mesas,escaleras,muebles"
This is wrong:
"Topes,4-Tipo=Topes,regatones",2-Familia=Ferretería y Plomería,regatones,7-Contenido="12 unidades,4-Origen=China,4-Material=Goma,2-Modelo=Goma transparente,9-Incluye=12 unidades,3-Color=Transparente"
Categoría="Topes,4-Tipo=Topes,regatones",2-Familia=Ferretería y Plomería,regatones,7-Contenido="12 unidades,4-Origen=China,4-Material=Goma,2-Modelo=Goma transparente,9-Incluye=12 unidades,3-Color=Transparente"
I´ve tried "|w+=" but selects all quotes. I don´t want to select text between quotes, the goal is select and remove these quotes.
We want to remove those quotes that contains an equal in between. The quotes that are ok and need to stay are those used to separate commas within the string, differentiating the variables from the string.
The regex needs to detect an = contained into and opening and closing quotes, but considering text in between. And once this is detected remove those quotes, which no need to be there.
Thanks!
I understand the quoted substring should be preceded with =. Then, you need
gsub('="([^"=]*=[^"]*)"', '=\\1', x)
See the R demo online:
x <- '10-Uso="Protector para patas de silla,mesas,escaleras,muebles",6-Características=Regaton interior 1 1/4 plástico blanco 4 unidades,1-Marca=Nagel,Tipo=Topes,5-Medidas=3 cm,3-Categoría=Topes y regatones,7-Contenido=4 unidades,4-Tipo=Regatones,2-Familia=Ferretería y Plomería,9-Incluye=4 regatones plásticos,regatones,4-Origen="Argentina,4-Material=Plástico,2-Modelo=Regatón interior 1 1/4,3-Color=Blanco"'
cat(gsub('="([^"=]*=[^"]*)"', '=\\1', x))
## => 10-Uso="Protector para patas de silla,mesas,escaleras,muebles",6-Características=Regaton interior 1 1/4 plástico blanco 4 unidades,1-Marca=Nagel,Tipo=Topes,5-Medidas=3 cm,3-Categoría=Topes y regatones,7-Contenido=4 unidades,4-Tipo=Regatones,2-Familia=Ferretería y Plomería,9-Incluye=4 regatones plásticos,regatones,4-Origen=Argentina,4-Material=Plástico,2-Modelo=Regatón interior 1 1/4,3-Color=Blanco
So, the quote after muebles is kept and quote after blanco is removed.
How does this work?
=" - matches =" substring
([^"=]*=[^"]*) - matches and captures into Group 1:
[^"=]* - zero or more chars other than " and =
= - a = sign
[^"]* - any 0+ chars other than "
" - matches ".
The replacement pattern is a = and the value stored in Group 1 memory buffer (\1, a replacement backreference).
See the regex demo.

R regex match things other than known characters

For a text field, I would like to expose those that contain invalid characters. The list of invalid characters is unknown; I only know the list of accepted ones.
For example for French language, the accepted list is
A-z, 1-9, [punc::], space, àéèçè, hyphen, etc.
The list of invalid charactersis unknown, yet I want anything unusual to resurface, for example, I would want
This is an 2-piece à-la-carte dessert to pass when
'Ã this Øs an apple' pumps up as an anomalie
The 'not contain' notion in R does not behave as I would like, for example
grep("[^(abc)]",c("abcdef", "defabc", "apple") )
(those that does not contain 'abc') match all three while
grep("(abc)",c("abcdef", "defabc", "apple") )
behaves correctly and match only the first two. Am I missing something
How can we do that in R ? Also, how can we put hypen together in the list of accepted characters ?
[a-z1-9[:punct:] àâæçéèêëîïôœùûüÿ-]+
The above regex matches any of the following (one or more times). Note that the parameter ignore.case=T used in the code below allows the following to also match uppercase variants of the letters.
a-z Any lowercase ASCII letter
1-9 Any digit in the range from 1 to 9 (excludes 0)
[:punct:] Any punctuation character
The space character
àâæçéèêëîïôœùûüÿ Any valid French character with a diacritic mark
- The hyphen character
See code in use here
x <- c("This is an 2-piece à-la-carte dessert", "Ã this Øs an apple")
gsub("[a-z1-9[:punct:] àâæçéèêëîïôœùûüÿ-]+", "", x, ignore.case=T)
The code above replaces all valid characters with nothing. The result is all invalid characters that exist in the string. The following is the output:
[1] "" "ÃØ"
If by "expose the invalid characters" you mean delete the "accepted" ones, then a regex character class should be helpful. From the ?regex help page we can see that a hyphen is already part of the punctuation character vector;
[:punct:]
Punctuation characters:
! " # $ % & ' ( ) * + , - . / : ; < = > ? # [ \ ] ^ _ ` { | } ~
So the code could be:
x <- 'Ã this Øs an apple'
gsub("[A-z1-9[:punct:] àéèçè]+", "", x)
#[1] "ÃØ"
Note that regex has a predefined, locale-specific "[:alpha:]" named character class that would probably be both safer and more compact than the expression "[A-zàéèçè]" especially since the post from ctwheels suggests that you missed a few. The ?regex page indicates that "[0-9A-Za-z]" might be both locale- and encoding-specific.
If by "expose" you instead meant "identify the postion within the string" then you could use the negation operator "^" within the character class formalism and apply gregexpr:
gregexpr("[^A-z1-9[:punct:] àéèçè]+", x)
[[1]]
[1] 1 8
attr(,"match.length")
[1] 1 1

symbols being printed when english alphabet wanted

I am writing this code and have recently come across an error. I have no idea why this is happening. In theory, the english alphabet should be being printed. However, instead of the english alphabet, symbols are being printed instead.
I can not paste the symbols for some reason, but if you ran the code yourself, you'll understand what I mean.
My full code is posted below.
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFHIJKLMNOPQRSTUVWXYZ0123456789"
choice = input("Would you like to encrypt or decrypt? [e/d]: ")
if choice == "e":
message = input("Please insert the message you would like to use: ")
keyword = input("Please insert the keyword you would like to use: ")
ik = len(keyword)
i = 0
string = ''
for A in message:
message1 = (ord(A)) - 96
key1 = (ord(keyword[i])) - 96
addition = message1 + key1
string += (chr(addition))
if i >= ik:
i = 0
else:
i += 1
print (string)
You need to add back the 96 you originally took away :) Alternatively, use the Caesar cipher formula as adding back 96 will still result in symbols appearing (I did the ocr coursework already)
addition = message1 + key1 + 96
your code will not work if the keyword is shorter than the message, so use the modulo operator (%) on i with the length of the keyword inside the line:
key1 = (ord(keyword[i])) - 96

Regular Expression To exclude sub-string name(job corps) Includes at least 1 upper case letter, 1 lower case letter, 1 number and 1 symbol except "#"

Regular Expression To exclude sub-string name(job corps)
Includes at least 1 upper case letter, 1 lower case letter, 1 number and 1 symbol except "#"
I have written something like below :
^((?!job corps).)(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!#$%^&*]).*$
I tested with the above regular expression, not working for special character.
can anyone guide on this..
If I understand well your requirements, you can use this pattern:
^(?![^a-z]*$|[^A-Z]*$|[^0-9]*$|[^!#$%^&*]*$|.*?job corps)[^#]*$
If you only want to allow characters from [a-zA-Z0-9^#$%&*] changes the pattern to:
^(?![^a-z]*$|[^A-Z]*$|[^0-9]*$|[^!#$%^&*]*$|.*?job corps)[a-zA-Z0-9^#$%&*]*$
details:
^ # start of the string
(?! # not followed by any of these cases
[^a-z]*$ # non lowercase letters until the end
|
[^A-Z]*$ # non uppercase letters until the end
|
[^0-9]*$
|
[^!#$%^&*]*$
|
.*?job corps # any characters and "job corps"
)
[^#]* # characters that are not a #
$ # end of the string
demo
Note: you can write the range #$%& like #-& to win a character.
stribizhev, your answer is correct
^(?!.job corps)(?=.[0-9])(?=.[a-z])(?=.[A-Z])(?=.[!#$%^&])(?!.#).$
can verify the expression in following url:
http://www.freeformatter.com/regex-tester.html

Generate Conditional Code From String

I want to generate conditional code of the string, which will check the string. An example is the following string
sampleString = "C123 A091 A111 A122 B120 B309 C000"
and examples of conditional string I have is as follows
example 1 :
A123 + B123 would I generate as
if sampleString.contains ("A123") And sampleString.contains ("B123") Then
'doSomething
else
'doSomething
end if
example 2 :
A111+A122+(B120/-C123)
if sampleString.contains ("A111") And sampleString.contains ("A122") And (sampleString.contains ("B120") Or Not sampleString.contains ("C123")) Then
'doSomething
else
'doSomething
end if
Plus (+) means AND
Minus (-) means NOT
Slash (/) means OR
Will I be able to do this in VB.Net?
Use Regex to solve the Contains problem by finding the matches and replace them with 1 or 0.
Dim sample As String = "C123 A091 A111 A122 B120 B309 C000"
Dim input As String = "A111 + A122 + ( B120/- C123)"
Dim nRegex As New Regex("\w+", RegexOptions.IgnoreCase)
Dim nMatches As MatchCollection = nRegex.Matches(input)
For Each nMatch As Match In nMatches
input = input.Replace(nMatch.Value, IIf(sample.Contains(nMatch.Value), "1", "0"))
Next
Now replace the special characters you have with ones that can actually give you the right result when evaluated as a math expression (Since logic is originally math).
input = input.Replace(" ", "")
input = input.Replace("+", "*")
input = input.Replace("/", "+")
input = input.Replace("-1", "0").Replace("-0", "1")
after all this you will get this expression :
1*1*(1+0)
You can now use any math evaluator as suggested in this Answer. If the result you get is bigger than zero means Its logically True. Otherwise False

Resources