Comments (6)
That's only need for perl compatible regexps, right?
from stringr.
Not entirely sure. The TRE POSIX 1003.2 library seems to have some bonus features. For example, the code above does appropriately capture what you would expect (i.e. just the ending group that gets the last letter), even though we use a non-capturing group to get the states that start with Ala or Mas only. Here is the version that uses regexec/regmatches:
lst.match <- regmatches(state.name, regexec("^(?:Ala|Mas).*(.)$", state.name))
lst.match[ifelse(sapply(lst.match, length) > 0, T, F)]
[[1]]
[1] "Alabama" "a"
[[2]]
[1] "Alaska" "a"
[[3]]
[1] "Massachusetts" "s"
Which works as expected (i.e. only the last letter is captured, but the non-capturing group works as it should). I haven't looked deeper in the source of str_match_*, but presumably they use regexec, so it seems this would work.
from stringr.
sorry, new to github, had no idea what "close & comment" did.
from stringr.
Just to follow up on this, it looks like the TRE ERE POSIX library definitely supports non capturing groups, in particular to specify options. See docs at: laurikari.net/tre/documentation/regex-syntax/. In particular the definition of atoms under section "Catenation of REs" which describes the following three as valid atoms:
- "(?#" comment-text ")"
- "(?" options ")" extended-regexp
- "(?" options ":" extended-regexp ")"
What isn't explicitly mentioned is whether the above types of groups/options are capturing, but based on the results above it seems they are not. I'll admit I'm not 100% familiar with the documentation syntax, so I may be misinterpreting.
As an aside, you would have to modify my fix to just remove any "(?" (as opposed to "(?:")from consideration as an indication of the number of matches, or have some other mechanism for detecting number of matches.
from stringr.
I've just fallen over this bug too. I was using R's internal regex engine rather than the Perl engine.
The fix I found was to locate all instances of open parentheses not followed by a question mark and a colon.
Replace the line
n <- str_length(str_replace_all(tmp, "[^(]", "")) + 1
with
n <- nrow(str_locate_all(pattern, perl("\\((?!\\?:)"))[[1]]) + 1
from stringr.
Fixed in stringi branch
from stringr.
Related Issues (20)
- str_replace_all with multiple replacements HOT 1
- stringr functions not working for strings with \r or \n HOT 7
- Wrong warning text for `str_view_all()` deprecation HOT 2
- Documentation edits in `split.R` Examples HOT 2
- Add helpers for `str_match()` HOT 2
- str_split simplify in mutate with native pipe - potentially untidy behaviour HOT 1
- Font is too big in the viewer from `str_view()` and `str_view_all()` HOT 1
- New features to easily capture text before or after n instance of delimiters without regex HOT 1
- Using a function as value for replacement argument in ```str_replace_all()``` HOT 1
- Adding a str_sub replacement in an R package, checking fails HOT 1
- `[[.stringr_pattern` method not defined
- Upkeep for stringr (2023)
- options(stringr.html = TRUE) does not work HOT 1
- Release stringr 1.5.1
- Problems with stringr and phyloseq
- str_view_all Shows Incorrect Error Message
- Add the .trim parameter to str_glue() HOT 1
- `str_detect_all` for multiple patterns? HOT 1
- Strings not properly splitting into separate list elements? HOT 3
- str_split not splitting correctly on Unicode character HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stringr.