OuhscBbmc / StatisticalComputing

OUHSC's SCUG (Statistical Computing Users Group)
MIT License
8 stars 3 forks source link

Morning SCUG Question #11

Closed Maleeha closed 4 years ago

Maleeha commented 4 years ago

@wibeasley

Here is the work on Regex101. Please forgive the amateur pattern of regex here:

Example on regex101

- Here is the pattern:

regex <- "(^\D\D\D\D)|-\D\D\D\D|(^\D+)3"


- Here is the execution in R:

sub(regex, "PF", pax) gsub(regex, "PF", pax)

- Here are the results with `sub`:

[1] "PFf2" "PF" "PFs4" "PFs5"
[5] "PFs6" "PF-podf-podf-podf-podf"

- ***Bottom-line: `sub` scans each cell and look at only the first match to replace it. Does not matter how much of the match was captured. Looks at matched and captured text the same way.***

- Here is an output with `gsub`

"PFf2" "PF" "PFs4" "PFs5" "PFs6" "PFPFPFPFPF"


- ***Bottom-line: `gsub` scans each cell and look at all the matches to replace them and then replace each match with substitution.***

Thank you for your patience this morning
wibeasley commented 4 years ago

Also correct.