Drop values between two characters in a string [duplicate] - regex

This question already has answers here:
get filename from url path in R
(2 answers)
Closed 7 years ago.
Suppose I have multiple forms of the following string
x <- "/Users/name/Google Drive/Thesis/Data/Data X and Y/UK//5port/5groups.csv"
I want to drop the values between the first / and the last / to return only 5groups.csv. I used gsub but couldn't find how to specify that pattern.
My question is the same like this one but in R.

If we are using sub
sub('.*\\/', '', x)
#[1] "5groups.csv"

Related

How to use pandas extract to remove string in a pandas column [duplicate]

This question already has an answer here:
Python Regular Expressions for a question mark [duplicate]
(1 answer)
Closed 10 months ago.
I have this dataframe:
d = pd.DataFrame({'test':['test?_Apple','test?_Banana', 'test?_limon']})
And I want to remove the test?_ string from test columns values to get this:
d = pd.DataFrame({'test':['Apple','Banana', 'limon']})
I am trying:
d['test'] = d['test'].str.extract(r'test?_(.*)')
but it returns NAN
Can someone help me on this one?
Escape ? because special regex character:
d['test'] = d['test'].str.extract(r'test\?_(.*)')

RegExp for Hangul in Ruby [duplicate]

This question already has answers here:
What is proper way to test if the input is Korean or Chinese using JavaScript?
(2 answers)
Closed 4 years ago.
How do you find Korean letters using regex in JavaScript?
You can use the following regex
const re = /[\u3131-\uD79D]/ugi
This is the code table that I referenced: http://memory.loc.gov/diglib/codetables/9.3.html
Try it yourself:
const re = /[\u3131-\uD79D]/ugi
console.log("abcde".match(re)) // null
console.log("안녕".match(re)) // ["안", "녕"]

Multiple instructions in Regex [duplicate]

This question already has answers here:
Regex using javascript to return just numbers
(14 answers)
Closed 3 years ago.
Suppose I have this text :
qsdfq fmld - 123 -mqlskdj -6464 - qlsdkjflj - 54654 -qsdfqsdf -2542
And I want to capture all numbers. A solution could be :
\D*(\d*)\D*(\d*)\D*(\d*)\D*(\d*)
But I don't know how many numbers I have. So I want to use the + sign.
I tried the following but it does not work :
[\D*(\d*)]+
You can simply use \d+ and global flag ( g )
let str = `qsdfq fmld - 123 -mqlskdj -6464 - qlsdkjflj - 54654 -qsdfqsdf -2542`
let op = str.match(/\d+/g)
console.log(op)
On side note: I have JS code just to show a working example.

How to replace this string with re.sub? [duplicate]

This question already has answers here:
python regular expression replacing part of a matched string
(5 answers)
Closed 4 years ago.
Assume two strings, foo.example.com/1.2.3/sdk-foo-bar.min.js and foo.example.com/1.2.3/sdk-foo-bar-dev.min.js.
By default, the first one is used in the HTML code, but depending on a parameter, I need to replace it with the second (i.e. add the -dev).
I have a regex already (foo\.example\.com/1\.2\.3/(sdk-foo-bar).min\.js) that looks for the string and captures the group sdk-foo-bar, but how can I now replace this group with sdk-foo-bar-dev??
inp = 'foo.example.com/1.2.3/sdk-foo-bar.min.js'
m = re.search('(^.*)(.min.js)', inp)
if m:
print ('%s-%s%s' % (m.group(1), 'dev', m.group(2)))

Finding all elements of a string vector that end with a given string in R [duplicate]

This question already has answers here:
Does R have function startswith or endswith like python? [closed]
(6 answers)
Closed 6 years ago.
Suppose I have a character vector:
test <- c("a##", "b", "c##", "d", "e")
I want to retrieve the indexes of elements ending with "##".
How can I do that?
Here is one method using grep:
grep("##$", test)
This returns indices 1 and 3 as a vector. The "##$" is a regular expression that says match if ## are the last two characters, the "$". anchors the ## to the end.