0

So, it's the code and I don't understand the output.

Original theStr:"C:\\Users\\codep\\Desktop\\DM_HW2\\2017-07-9.csv"

gsub("^(.*)\\\\.*$",'\\1',theStr)

it become:"C:\\Users\\codep\\Desktop\\DM_HW2"

what is the "\\\\." in the pattern and the '\\1' in the replacement?

  • https://stackoverflow.com/questions/4736/learning-regular-expressions – jogo Nov 07 '18 at 07:49
  • You can use a mere `sub` here, you only perform a single replace operation. `^(.*)\\.*$` can be well-explained [at regex101.com](https://regex101.com/r/wHHOTn/1). – Wiktor Stribiżew Nov 07 '18 at 07:58

1 Answers1

0

Your pattern may be explained as follows:

^(.*)\\\\   - match and capture everything up but excluding the LAST path separator
.*$         - then match/consume the remainder of the file path

Then, you replace the original input with the captured quantity, which is \\1, the second parameter passed to gsub. This effectively removes everything from the final path separator to the end of the file path.

Here is a regex demo which you can use to see for yourself how the pattern is matching, and what the capture group is:

Demo

Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263