Write a regular expression to match phone numbers [duplicate] - r

This question already has answers here:
Extract phone number regex
(3 answers)
Closed 4 years ago.
I have to write a regular expression to match phone numbers. A phone number can be in any one of the following forms:
(123) 456-7890
(123)456-7890
1234567890
123 456 7890
123-456-7890
123.456.7890
I have to store the regular expression in a variable called re5.
The test cases I have to pass are here: link
I just need the re5 = "..." part, where I need the correct code for the ... part.
This is what I have so far..
re5 = "^(\\d{3}|\\(\\d{3}\\))([ ]?\\d{3}[-]|[ ]\\".
I don't know what is going wrong. Thanks!
Different that other answers due to test cases in link above.

This passes all your tests:
^(?:\(\d{3}\)|\d{3})(?: |([-.])?)\d{3}(?(1)\1|[ -.]?)\d{4}$
Bear in mind that this uses PCRE-specific syntax, so you'll need to enable that using perl = TRUE

Related

Why "R help" does not work for some commands? [duplicate]

This question already has answers here:
How to get help in R?
(6 answers)
Closed 2 years ago.
I wanted to use "help in R" in order to see some information about some commands such as "for", "if", "while", "repeat" etc. But there is no information in "R help" regarding such commands. I would like to know why?
I use "R help" for the above commands like below:
?for
?while
?if
?repeat
R requires that keywords are used in syntactically valid form. The way R works, it expects that if for instance is followed by an expression in parentheses, and a body. ?if is not valid R syntax.
Conversely, ? is an operator that expects an identifier after it.
To make it valid, you should quote the if identifier in backticks. That way, R parses the expression as ? followed by an identifier, rather than ? followed by an incomplete if expression:
?`if`
Backtick-quoting is R’s way of saying: “hey, that thing between backticks is a valid identifier, even if it totally doesn’t look like one”. You could (but generally shouldn’t!) totally use it to use wonky variable names:
`name with spaces` = 2
message(`name with spaces` + 5)
# 7
This feature is more useful when applied to column names of externally imported data (which sometimes contains spaces or other invalid identifier characters), or when defining operators.

how to get the last part of strings with different lengths ended by ".nc" [duplicate]

This question already has answers here:
Get filename without extension in R
(9 answers)
Find file name from full file path
(4 answers)
Closed 3 years ago.
I have several download links (i.e., strings), and each string has different length.
For example let's say these fake links are my strings:
My_Link1 <- "http://esgf-data2.diasjp.net/pr/gn/v20190711/pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231.nc"
My_Link2 <- "http://esgf-data2.diasjp.net/gn/v20190711/pr_-present_r1i1p1f1_gn_19500101-19591231.nc"
My goals:
A) I want to have only the last part of each string ended by .nc , and get these results:
pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231.nc
pr_-present_r1i1p1f1_gn_19500101-19591231.nc
B) I want to have only the last part of each string before .nc , and get these results:
pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231
pr_-present_r1i1p1f1_gn_19500101-19591231
I tried to find a way on the net, but I failed. It seems this can be done in Python as documented here:
How to get everything after last slash in a URL?
Does anyone know the same method in R?
Thanks so much for your time.
A shortcut to get last part of the string would be to use basename
basename(My_Link1)
#[1] "pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231.nc"
and for the second question if you want to remove the last ".nc" we could use sub like
sub("\\.nc", "", basename(My_Link1))
#[1] "pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231"
With some regex here is another way to get first part :
sub(".*/", "", My_Link1)

Ignore regular expression in R with gsub [duplicate]

This question already has answers here:
How do I deal with special characters like \^$.?*|+()[{ in my regex?
(2 answers)
How to escape a question mark in R?
(4 answers)
Closed 3 years ago.
df <- data.frame(
videos = c("Moon vs Grubby", "Moon vs Happy", "Happy vs Th00"),
links = c("https://www.youtube.com/watch?v=QlNc-jb4ESk&t", "https://www.youtube.com/watch?v=VESO8YQVFSE", "https://www.youtube.com/watch?v=RI3IJT8ZzBM")
)
df$links <- as.character(df$links)
df$links <- gsub("watch?v=", "embed/", df$links)
I have got the following code with links to YouTube which I want to embed in a shiny App. However YouTube needs to replace part of the string which is interpreted as a regular expression. I did not find a helpful solution here.
So how can I gsub this pattern?
Current Links:
https://www.youtube.com/watch?v=QlNc-jb4ESk&t
Expected Outcome:
https://www.youtube.com/embed/=QlNc-jb4ESk&t
We need to escape the ? and = as these are metacharacters
gsub("watch\\?v\\=", "embed/=", df$links)
or with fixed = TRUE
gsub("watch?v=", "embed/=", df$links, fixed = TRUE)
Also, as there is only a single instance, we can use sub
sub("watch?v=", "embed/=", df$links, fixed = TRUE)
#[1] "https://www.youtube.com/embed/=QlNc-jb4ESk&t"
#[2] "https://www.youtube.com/embed/=VESO8YQVFSE"
#[3] "https://www.youtube.com/embed/=RI3IJT8ZzBM"
My guess is that this expression might work:
(\S*)watch\?v=(\S*)
The expression is explained on the top right panel of this demo, if you wish to explore further or modify it, and in this link, you can watch how it would match against some sample inputs step by step, if you like.
and our code might look like:
gsub("(\\S*)watch\\?v\\=(\\S*)", "\\1embed/\\2", df$links)
My guess is that this would be the desired output:
https://www.youtube.com/embed/QlNc-jb4ESk&t

SQLITE3 + execute Insert [duplicate]

This question already has answers here:
SQLite parameter substitution problem
(8 answers)
Closed 7 years ago.
Trying to execute insert an item coming from a list:`
item=u'Sunil Goyal'
c.execute('''INSERT INTO bpersons(person_name) VALUES (?)''',item)`
is simple enough, but it returns
Incorrect number of bindings supplied. The current statement uses 1, and there are 11 supplied.
Clearly instead of reading item as one element, it is reading characters. There is no problem with the earlier code which returns this list:
>>> if meta[7]:#bcoz list could be empty also
for item in meta[7]:
print item
Sunil Goyal
Rehan Yar Khan
Khan
Kae Capital
Ashish Shankar
Karthik Reddy
Feroze Azeez
len(meta[7])
7
Any idea where I am going wrong?
insert is looking for an iterable (documentation) and this succeeds because your unicode string is an iterable, but you should put it inside of a tuple or list to be handled properly by sqlite3.
c.execute('''INSERT INTO bpersons(person_name) VALUES (?)''',(item,))`

Regular expression for 6 characters [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I need couple of regular expression in ASP.NET.
First should accept only 6 characters and second and third character should accept only _ (underscore).
Like: a__cde
And I want one other regex that should also take 6 characters and at second position it should accept only underscore (_) and at third position it should accept maybe underscore (_) or hash (#) and at fourth position it should accept only hash (#).
Note: In both the regex user can only enter: Number, Alphabets or Star (*) at any position instead of above mentioned positions.
Can any one help me out on this? I have tried by below website:
http://www.regexr.com/
But not able to generate proper regex.
Try something like:
1. ^[a-zA-Z0-9\*]__[a-zA-Z0-9\*]{3}$
2. ^[a-zA-Z0-9\*]_[_#]#[a-zA-Z0-9\*]{2}$

Resources