How do i write a regex for verifying a string that contains exactly 4 alpha 7 numeric characters respectively - string-matching

I have used everything I can find but nothing is working - the result is always false when i know it should be true. example of string format that needs to be verified: TGHU4674045
/A-Z{4}\d{7}/
/[(A-Z{4}0-9{7})]/
^(?=(?:\\d*\\D\\d*){4}$)[A-Za-z0-9]{7}$
/^[a-zA-Z0-9]{4}[0-9]{7}$/
^(A-Z{4})[A-Za-z0-9]{11}$

^[A-Z][A-Z][A-Z][A-Z][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$
or
^[A-Z]{4}[0-9]{7}$

Related

Crazy unexpected behavior of grepl

What explains the following very unexpected behavior of grepl?
I am using grepl for basic string matching here, and I think the default behavior as illustrated below is dangerous.
> grepl('a','a')
[1] TRUE
> grepl('a ()','a ()')
[1] TRUE
> grepl('a (b)','a (b)')
[1] FALSE
Adding fixed=TRUE fixes it. The documentation says:
pattern: character string containing a regular expression (or character string for fixed = TRUE) to be matched in the given character vector.
The average user should get from the message above that the default usage of grepl is NOT string matching but regular expression matching, which is not super clear. Someone unaware of regular expressions may not realize the dangers of leaving fixed to its default value. I think a warning should be added about this.
Posting here mainly to alert the community about this behavior. It took me a couple of hours of debugging to narrow down the issue I was experiencing in my Shiny app to this function. I would have never thought that grepl could be dangerous like this.
pattern: a ()
Breakdown: An a followed by a space and then a captured null/empty character ie Nothing.
The a and space matches the first part of the string. Thus the WHOLE pattern can be found in the string. RESULTS in TRUE
second part:
pattern: a (b)
Breakdown. Literally means a b ie a then space then b. But we capture the b hence the parenthesis around b.
String has a (b). Since b does not follow the space, the whole pattern cannot be obtained in the string hence FALSE

I need help figuring out why my regex does not match with what I am looking for

I am working on a R script aiming to check if a data.frame is correctly made and contains the right information at the right place.
I need to make sure a row contains the right information, so I want to use a regular expression to compare with each case of said row.
I thought maybe it did not work because I compared the regex to the value by calling the value directly from the table, but it did not work.
I used regex101.com to make sure my regular expression was correct, and it matched when the test string was put between quotes.
Then I added as.character() to the value, but it came out FALSE.
To sum up, the regex works on regex101.com, but never did on my R script
test = c("b40", "b40")
".[ab][0-8]{2}." == test[1]
FALSE
I expect the output to be TRUE, but it is always FALSE
The == is for fixed full string match and not used for substring match. For that, we can use grep
grepl("^[ab][0-8]{2}", test[1])
#[1] TRUE
Here, we match either 'a' or 'b' at the start (^) of the string followed by two digits ranging from 0 to 8 (if it should be at the end - use $)

grepping special characters in R

I have a variable named full.path.
And I am checking if the string contained in it is having certain special character or not.
From my code below, I am trying to grep some special character. As the characters are not there, still the output that I get is true.
Could someone explain and help. Thanks in advance.
full.path <- "/home/xyz"
#This returns TRUE :(
grepl("[?.,;:'-_+=()!##$%^&*|~`{}]", full.path)
By plugging this regex into https://regexr.com/ I was able to spot the issue: if you have - in a character class, you will create a range. The range from ' to _ happens to include uppercase letters, so you get spurious matches.
To avoid this behaviour, you can put - first in the character class, which is how you signal you want to actually match - and not a range:
> grepl("[-?.,;:'_+=()!##$%^&*|~`{}]", full.path)
[1] FALSE

RegEx Replace Automatic SQL code creator

I built a script in R that automatically create a very long and complex SQL query to create a view over similar tables of 5 databases.
Of course there were integration issues to solve. The only one remaining to make this happen is the problem I am going to present you now.
Considering one very long string like
'"/*NOTES*/", "/*TABLE_ID*/", "/*TABLE_SUB_ID*/", "/*TABLE_SUB_SUB_ID*/", "OTHER_COLUMNS",'
My objective is to replace
this string '"/*' with this string '/*'
this string '*/",' with this string '*/'
I tried with:
gsub('"/*', '/*', '"/*NOTES*/", "/*TABLE_ID*/", "/*TABLE_SUB_ID*/", "/*TABLE_SUB_SUB_ID*/", "OTHER_COLUMNS",')
but it returns the string
'/**NOTES*//*, /**TABLE_ID*//*, /**TABLE_SUB_ID*//*, /**TABLE_SUB_SUB_ID*//*, /*OTHER_COLUMNS/*,'
whereas my expected output is the following string:
'/*NOTES*/ /*TABLE_ID*/ /*TABLE_SUB_ID*/ /*TABLE_SUB_SUB_ID*/ "OTHER_COLUMNS",'
Note the * is not escaped but it represents start (/*) and end (*/) of comments when the string will be run by a SQL compiler
Escaping regexes requires two backslashes, so the following will get you what you want:
gsub('"?(/\\*|\\*/)"?', '\\1', '"/*NOTES*/", "/*TABLE_ID*/", "/*TABLE_SUB_ID*/", "/*TABLE_SUB_SUB_ID*/", "OTHER_COLUMNS",')
# [1] "/*NOTES*/, /*TABLE_ID*/, /*TABLE_SUB_ID*/, /*TABLE_SUB_SUB_ID*/, \"OTHER_COLUMNS\","
FYI, double-backslashes are required for most, but the following are legitimate single-backslash special characters:
'\a\b\f\n\r\t\v'
# [1] "\a\b\f\n\r\t\v"
'\u0101' # unicode, numbers are variable
# [1] "a"
'\x0A' # hex, hex-numbers are variable
# [1] "\n"
Perhaps there are more, I didn't find the authoritative list though I'm sure it's in there somewhere.

How to compare a variable to two values, by using OR condition in robot framework?

I want to compare ${accountNumChk} should be equal 6 or 7 .
should be equal as strings ${accountNumChk} 6
Anyone answer please .
This can't be done with Should be equal. You can use a workaround with Should Be True:
Should Be True '${accountNumChk}'=='6' or '${accountNumChk}'=='7'
Note that or should be lowercase.
#Psytho's solution is good (and probably optimal); for the sake of having alternatives, here's one with only the standard keywords:
${check1}= Run Keyword And Return Status Should Be Equal As Strings ${accountNumChk} 6
Run Keyword If not ${check1} Should Be Equal As Strings ${accountNumChk} 7
Run Keyword And Return Status returns boolean True or False depending if the enclosed keyword succeeded or failed. Run Keyword If executes the enclosed keyword only if its check evaluates to True.

Resources