1

I have a tibble with a variable df$sec which is a vector of two character strings.

sec <- c("21", "33", "24", "7-", "32", "1-")  

The character set 7- and 1- should be 07 and 01.

Using {stringr}

df %>% 
  mutate(section = str_replace(string=sec, pattern = ".-$", "0?")) %>% 
  select(section)  

This returns: 21,33,24,0?, 32, 0?
How do I return the characters 7- and 1- as 07 and 01?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Robert Cline
  • 37
  • 1
  • 7
  • Try `str_replace(string=sec, pattern = "^(\\d)-$", "0\\1")` – Wiktor Stribiżew Aug 04 '21 at 23:19
  • I am trying to understand the grouping. I've just ordered a couple of books on regex. If you have suggestions, please post. Thank you again. – Robert Cline Aug 05 '21 at 19:04
  • I can only suggest doing all lessons at [regexone.com](http://regexone.com/), reading through [regular-expressions.info](http://www.regular-expressions.info), [regex SO tag description](http://stackoverflow.com/tags/regex/info) (with many other links to great online resources), and the community SO post called [What does the regex mean](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean). Also, [rexegg.com](http://rexegg.com) is worth having a look at. For R, also see [my answer](https://stackoverflow.com/a/47251004/3832970). – Wiktor Stribiżew Aug 05 '21 at 19:11

1 Answers1

1

You can use

str_replace(string=sec, pattern = "^(\\d)-$", "0\\1")

The regex matches

  • ^ - start of string
  • (\d) - capturing group #1 (\1 in the replacement pattern refers to the string captured in this group): a digit
  • - - a hyphen
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563