Skip to content

Commit

Permalink
Add more examples and prepare release
Browse files Browse the repository at this point in the history
  • Loading branch information
qinwf committed Aug 29, 2017
1 parent 17ead2c commit bfecd88
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ GTAGS
.git
\.DS_store
^cran-comments\.md$
^inst/img/number\.png$
^number\.png$
13 changes: 13 additions & 0 deletions R/match.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@
#' "One Direction hit the road agains"
#' )
#' re2_match_all(texts, pattern)
#'
#' texts = c("pi is 3.14529..",
#' "-15.34 °F",
#' "128 days",
#' "1.9e10",
#' "123,340.00$",
#' "only texts")
#' (number_pattern = re2(".*?(?P<number>-?\\d+(,\\d+)*(\\.\\d+(e\\d+)?)?).*?"))
#'
#' (res = re2_match(texts, number_pattern))
#' res$number
#'
#' # show_regex(number_pattern)
#' @return For \code{\link{re2_match}}, a character matrix. First column
#' is the complete match, followed by one column
#' for each capture group with names.
Expand Down
15 changes: 15 additions & 0 deletions R/replace.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@
#' # trim string
#' pattern = "^\\s+|\\s+$"
#' re2_replace_all(c(" abc "," this is "), pattern, "")
#'
#' # mask the middle three digits of a US phone number
#' texts = c("415-555-1234",
#' "650-555-2345",
#' "(416)555-3456",
#' "202 555 4567",
#' "4035555678",
#' "1 416 555 9292")
#'
#' us_phone_pattern = re2("(1?[\\s-]?\\(?\\d{3}\\)?[\\s-]?)(\\d{3})([\\s-]?\\d{4})")
#'
#' re2_replace(texts, us_phone_pattern, "\\1***\\3")
#'
#' # show_regex(us_phone_pattern)
#'
#' @return For \code{\link{re2_replace}}, a character vector. For \code{\link{re2_replace_all}}, a character vector with the number of replacements.
#' @export
re2_replace = function(string,
Expand Down
79 changes: 64 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ re2_detect("[email protected]", "\\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4

## .match .1
## [1,] "one" "one"
## attr(,"class")
## [1] "re2_matrix"

The return result is a character matrix. `.1` is the first capture group and it is unnamed group.

Expand All @@ -77,8 +75,6 @@ Create named capture group with `(?P<name>pattern)` syntax.

## .match testname .2
## [1,] "this is" "this" " is"
## attr(,"class")
## [1] "re2_matrix"

``` r
is.matrix(res)
Expand Down Expand Up @@ -108,9 +104,7 @@ test_string = c("this is just one test", "the second test");

## .match
## [1,] "is"
## [2,] NA
## attr(,"class")
## [1] "re2_matrix"
## [2,] NA

`re2_match_all()` will return the all of patterns in a string instead of just the first one.

Expand All @@ -125,27 +119,66 @@ print(res)
## [[1]]
## .match testname .2
## [1,] "this is" "this" " is"
## attr(,"class")
## [1] "re2_matrix"
##
## [[2]]
## .match testname .2
## [1,] "this is" "this" " is"
## [2,] "this is" "this" " is"
## attr(,"class")
## [1] "re2_matrix"
##
## [[3]]
## .match testname .2
## attr(,"class")
## [1] "re2_matrix"

``` r
is.list(res)
```

## [1] TRUE

match all numbers

``` r
texts = c("pi is 3.14529..",
"-15.34 °F",
"128 days",
"1.9e10",
"123,340.00$",
"only texts")
(number_pattern = re2(".*?(?P<number>-?\\d+(,\\d+)*(\\.\\d+(e\\d+)?)?).*?"))
```

## re2 pre-compiled regular expression
##
## pattern: .*?(?P<number>-?\d+(,\d+)*(\.\d+(e\d+)?)?).*?
## number of capturing subpatterns: 4
## capturing names with indices:
## .match number .2 .3 .4
## expression size: 56

``` r
(res = re2_match(texts, number_pattern))
```

## .match number .2 .3 .4
## [1,] "pi is 3.14529" "3.14529" NA ".14529" NA
## [2,] "-15.34" "-15.34" NA ".34" NA
## [3,] "128" "128" NA NA NA
## [4,] "1.9e10" "1.9e10" NA ".9e10" "e10"
## [5,] "123,340.00" "123,340.00" ",340" ".00" NA
## [6,] NA NA NA NA NA

``` r
res$number
```

## [1] "3.14529" "-15.34" "128" "1.9e10" "123,340.00"
## [6] NA

``` r
show_regex(number_pattern)
```

![number pattern](https://raw.githubusercontent.com/qinwf/re2r/master/inst/img/number.png)

### 2. Replace a substring

``` r
Expand All @@ -162,6 +195,24 @@ re2_replace(new_string, "(o.e)", input_string)

## [1] "my"

mask the middle three digits of a US phone number

``` r
texts = c("415-555-1234",
"650-555-2345",
"(416)555-3456",
"202 555 4567",
"4035555678",
"1 416 555 9292")

us_phone_pattern = re2("(1?[\\s-]?\\(?\\d{3}\\)?[\\s-]?)(\\d{3})([\\s-]?\\d{4})")

re2_replace(texts, us_phone_pattern, "\\1***\\3")
```

## [1] "415-***-1234" "650-***-2345" "(416)***-3456" "202 *** 4567"
## [5] "403***5678" "1 416 *** 9292"

### 3. Extract a substring

``` r
Expand Down Expand Up @@ -208,8 +259,6 @@ re2_match("TEST", regexp)

## .match
## [1,] "TEST"
## attr(,"class")
## [1] "re2_matrix"

``` r
re2_replace("TEST", regexp, "ops")
Expand Down
35 changes: 35 additions & 0 deletions README.rmd
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,27 @@ print(res)
is.list(res)
```

match all numbers

```{r}
texts = c("pi is 3.14529..",
"-15.34 °F",
"128 days",
"1.9e10",
"123,340.00$",
"only texts")
(number_pattern = re2(".*?(?P<number>-?\\d+(,\\d+)*(\\.\\d+(e\\d+)?)?).*?"))
(res = re2_match(texts, number_pattern))
res$number
```

```r
show_regex(number_pattern)
```

![number pattern](https://raw.githubusercontent.com/qinwf/re2r/master/inst/img/number.png)

### 2. Replace a substring

```r
Expand All @@ -109,6 +130,20 @@ new_string = "my"
re2_replace(new_string, "(o.e)", input_string)
```

mask the middle three digits of a US phone number

```{r}
texts = c("415-555-1234",
"650-555-2345",
"(416)555-3456",
"202 555 4567",
"4035555678",
"1 416 555 9292")
us_phone_pattern = re2("(1?[\\s-]?\\(?\\d{3}\\)?[\\s-]?)(\\d{3})([\\s-]?\\d{4})")
re2_replace(texts, us_phone_pattern, "\\1***\\3")
```
### 3. Extract a substring

```r
Expand Down
Binary file added inst/img/number.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions man/re2_match.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions man/re2_replace.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bfecd88

Please sign in to comment.