Giter VIP home page Giter VIP logo

Comments (6)

hadley avatar hadley commented on May 31, 2024

That's only need for perl compatible regexps, right?

from stringr.

smilberg avatar smilberg commented on May 31, 2024

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.

smilberg avatar smilberg commented on May 31, 2024

sorry, new to github, had no idea what "close & comment" did.

from stringr.

smilberg avatar smilberg commented on May 31, 2024

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.

richierocks avatar richierocks commented on May 31, 2024

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.

hadley avatar hadley commented on May 31, 2024

Fixed in stringi branch

from stringr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.