Ad
How To Use `strsplit` Before Every Capital Letter Of A Camel Case?
I want to use strsplit
at a pattern before every capital letter and use a positive lookahead. However it also splits after every, and I'm confused about that. Is this regex incompatible with strsplit
? Why is that so and what is to change?
strsplit('AaaBbbCcc', '(?=\\p{Lu})', perl=TRUE)[[1]]
strsplit('AaaBbbCcc', '(?=[A-Z])', perl=TRUE)[[1]]
strsplit('AaaBbbCcc', '(?=[ABC])', perl=TRUE)[[1]]
# [1] "A" "aa" "B" "bb" "C" "cc"
Expected result:
# [1] "Aaa" "Bbb" "Ccc"
In the Demo it actually looks fine.
Ideally it should split before every camel case, e.g. Aa
and not AA
; there's \\p{Lt}
but this doesn't seem to work at all.
strsplit('AaaABbbBCcc', '(?=\\p{Lt})', perl=TRUE)[[1]]
# [1] "AaaABbbBCcc"
Expected result:
# [1] "AaaA" "BbbB" "Ccc"
Ad
Answer
It seems that by adding (?!^)
you can obtained the desired result.
strsplit('AaaBbbCcc', "(?!^)(?=[A-Z])", perl=TRUE)
For the camel case we may do
strsplit('AaaABbbBCcc', '(?!^)(?=\\p{Lu}\\p{Ll})', perl=TRUE)[[1]]
strsplit('AaaABbbBCcc', '(?!^)(?=[A-Z][a-z])', perl=TRUE)[[1]] ## or
# [1] "AaaA" "BbbB" "Ccc"
Ad
source: stackoverflow.com
Related Questions
- → OctoberCMS Backend Loging Hash Error
- → "failed to open stream" error when executing "migrate:make"
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → Create plugin that makes objects from model in back-end
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → How to install console for plugin development in October CMS
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → How to update data attribute on Ajax complete
- → October CMS - Conditionally Load a Different Page
Ad