First two letters from words Regex in R

Clemsang

I am trying to get the first upper and lower letter from each word from a string.

string<-"Programmation _ Is 2 Cool"
gsub("[^A-Z]", "", string)
gsub("[^A-Za-z]", "", string)

The two results are :

"PIC"
"ProgrammationIsCool"

I would like to get :

"PrIsCo"

Thanks for help

Wiktor Stribiżew

If the first uppercase and the next lowercase letters must be extracted, use

(\\b[A-Z][a-z])|.

or

(\\b\\p{Lu}\\p{Ll})|.

The idea is to match and capture first uppercase and the following lowercase letters, and remove all the rest.

gsub("(\\b[A-Z][a-z])|.", "\\1", string, perl=TRUE)

Note that to remove newlines, you will need to pre-pend (?s) to the beginning of the pattern.

Pattern details:

  • (\\b[A-Z][a-z]) - Group 1 matching
    • \\b - a word boundary
    • [A-Z][a-z] - An uppercase ASCII letter followed with a lowercase ASCII letter (replace with \\p{Lu}\\p{Ll} to match any Unicode uppercase-lowercase letters).
  • | - or
  • . - any character but a newline

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regex to extract first 3 words from a string

From Dev

Regex Capturing: repeating two letters

From Dev

Regex Checking if String Contains Two or More Instances of Words from a Set

From Dev

Using R to compare two words and find letters unique to second word (across c. 6000 cases)

From Dev

Regex for two words

From Dev

python regex two words

From Dev

Extracting first two words of a string in Javascript using regex

From Dev

How do I select first N letters from words longer than M letters with regexp?

From Dev

Get first letters from a string of two words (PHP - fastest way)

From Dev

Find all words with 3 letters with regex

From Dev

String that contains first letters of words in another String

From Dev

Extracting everything after first two words in R

From Dev

REGEX in R: extracting words from a string

From Dev

Using regex in Java to extract string after first comma and before two capital letters and a comma

From Dev

How to match strings with regex to know of the first two words of a letter has their first later capitalised

From Dev

Regex for all 10 letter words, with unique letters

From Dev

Replace letters with Words using JavaScript Regex

From Dev

Regex Capturing: repeating two letters

From Dev

Regex Checking if String Contains Two or More Instances of Words from a Set

From Dev

Using R to compare two words and find letters unique to second word (across c. 6000 cases)

From Dev

If a cell contains two words, how would I use the first letter of the first word but use all the letters of the second word?

From Dev

Combining first two letters from first name and first two letters from last name

From Dev

Regex check for words and words with spaces separating letters

From Dev

How do I select first N letters from words longer than M letters with regexp?

From Dev

Rearrange the letters and Compare two words

From Dev

RegEx for first letters that MIGHT precede @

From Dev

Remove words from file which has two or more of the same letters letters in a row

From Dev

Regex - Allow capital letters just at the beginning of words

From Dev

Changing the first letters of words in a string to uppercase in javascript

Related Related

  1. 1

    Regex to extract first 3 words from a string

  2. 2

    Regex Capturing: repeating two letters

  3. 3

    Regex Checking if String Contains Two or More Instances of Words from a Set

  4. 4

    Using R to compare two words and find letters unique to second word (across c. 6000 cases)

  5. 5

    Regex for two words

  6. 6

    python regex two words

  7. 7

    Extracting first two words of a string in Javascript using regex

  8. 8

    How do I select first N letters from words longer than M letters with regexp?

  9. 9

    Get first letters from a string of two words (PHP - fastest way)

  10. 10

    Find all words with 3 letters with regex

  11. 11

    String that contains first letters of words in another String

  12. 12

    Extracting everything after first two words in R

  13. 13

    REGEX in R: extracting words from a string

  14. 14

    Using regex in Java to extract string after first comma and before two capital letters and a comma

  15. 15

    How to match strings with regex to know of the first two words of a letter has their first later capitalised

  16. 16

    Regex for all 10 letter words, with unique letters

  17. 17

    Replace letters with Words using JavaScript Regex

  18. 18

    Regex Capturing: repeating two letters

  19. 19

    Regex Checking if String Contains Two or More Instances of Words from a Set

  20. 20

    Using R to compare two words and find letters unique to second word (across c. 6000 cases)

  21. 21

    If a cell contains two words, how would I use the first letter of the first word but use all the letters of the second word?

  22. 22

    Combining first two letters from first name and first two letters from last name

  23. 23

    Regex check for words and words with spaces separating letters

  24. 24

    How do I select first N letters from words longer than M letters with regexp?

  25. 25

    Rearrange the letters and Compare two words

  26. 26

    RegEx for first letters that MIGHT precede @

  27. 27

    Remove words from file which has two or more of the same letters letters in a row

  28. 28

    Regex - Allow capital letters just at the beginning of words

  29. 29

    Changing the first letters of words in a string to uppercase in javascript

HotTag

Archive