[R] Regular expression help

Duncan Murdoch murdoch.duncan at gmail.com
Mon Oct 9 18:23:39 CEST 2017


On 09/10/2017 12:06 PM, William Dunlap wrote:
> "(^| +)([^/ ]*/?){0,2}", with the first "*" replaced by "+" would be a 
> bit better.

Thanks!  I think I actually need the *, because theoretically the b part 
of the word could be empty, i.e. "a//c" would be legal and should become 
"c".

Duncan Murdoch

> 
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com <http://tibco.com>
> 
> On Mon, Oct 9, 2017 at 8:50 AM, William Dunlap <wdunlap at tibco.com 
> <mailto:wdunlap at tibco.com>> wrote:
> 
>      > x <- "f 147/1315/587 2820/1320/587 3624/1321/587 1852/1322/587"
>      > gsub("(^| *)([^/ ]*/?){0,2}", "\\1", x)
>     [1] " 587 587 587 587"
>      > y <- "aa aa/ aa/bb aa/bb/ aa/bb/cc aa/bb/cc/ aa/bb/cc/dd
>     aa/bb/cc/dd/"
>      > gsub("(^| *)([^/ ]*/?){0,2}", "\\1", y)
>     [1] "    cc cc/ cc/dd cc/dd/"
> 
> 
>     Bill Dunlap
>     TIBCO Software
>     wdunlap tibco.com <http://tibco.com>
> 
>     On Mon, Oct 9, 2017 at 8:02 AM, Duncan Murdoch
>     <murdoch.duncan at gmail.com <mailto:murdoch.duncan at gmail.com>> wrote:
> 
>         I have a file containing "words" like
> 
> 
>         a
> 
>         a/b
> 
>         a/b/c
> 
>         where there may be multiple words on a line (separated by
>         spaces).  The a, b, and c strings can contain non-space,
>         non-slash characters. I'd like to use gsub() to extract the c
>         strings (which should be empty if there are none).
> 
>         A real example is
> 
>         "f 147/1315/587 2820/1320/587 3624/1321/587 1852/1322/587"
> 
>         which I'd like to transform to
> 
>         " 587 587 587 587"
> 
>         Another real example is
> 
>         "f 1067 28680 24462"
> 
>         which should transform to "   ".
> 
>         I've tried a few different regexprs, but am unable to find a way
>         to say "transform words by deleting everything up to and
>         including the 2nd slash" when there might be zero, one or two
>         slashes.  Any suggestions?
> 
>         Duncan Murdoch
> 
>         ______________________________________________
>         R-help at r-project.org <mailto:R-help at r-project.org> mailing list
>         -- To UNSUBSCRIBE and more, see
>         https://stat.ethz.ch/mailman/listinfo/r-help
>         <https://stat.ethz.ch/mailman/listinfo/r-help>
>         PLEASE do read the posting guide
>         http://www.R-project.org/posting-guide.html
>         <http://www.R-project.org/posting-guide.html>
>         and provide commented, minimal, self-contained, reproducible code.
> 
> 
>



More information about the R-help mailing list