2

How you would extract only the / with the following capital letters, and the whole [[:punct:]]/$[[:punct:]].

text <- c("This/ART ,/$; Is/NN something something/else A/VAFIN faulty/ADV text/ADV which/ADJD i/PWS propose/ADV as/APPR Example/NE ./$. So/NE It/PTKNEG makes/ADJD no/VAFIN sense/ADV at/KOUS all/PDAT ,/$, it/APPR Has/ADJA Errors/NN  ,/$; and/APPR it/APPR is/CARD senseless/NN again/ART ./$:")

# HOW to?
textPOS <- strsplit(text,"(   )|(?<=[[:punct:]]/\\$[[:punct:]])", perl=TRUE)
#                          ^^^ 
#                         extract only the "/" with the following capital letters
#                         and the whole "[[:punct:]]/$[[:punct:]]"

# Expected RETURN:
> textPOS
[1] "/ART" ",/$;" "/NN" "/VAFIN" "/ADV" "/ADV" "/ADJD" "/PWS" "/ADV" "/APPR" "/NE" "./$." "/NE" "/PTKNEG" "/ADJD" "/VAFIN" "/ADV" "/KOUS" "/PDAT" ",/$," "/APPR" "/ADJA" "/NN" ",/$;" "/APPR" "/APPR" "/CARD" "/NN" "/ART" "./$:"

Thank you! :)

4

1 回答 1

7

您可以使用gregexprregmatches

regmatches(text, gregexpr('[[:punct:]]*/[[:alpha:][:punct:]]*', text))
# [[1]]
#  [1] "/ART"    "/NN"     "/VAFIN"  "/ADV"    "/ADV"    "/ADJD"   "/PWS"    "/ADV"    "/APPR"   "/NE"     "./$."    "/NE"    
# [13] "/PTKNEG" "/ADJD"   "/VAFIN"  "/ADV"    "/KOUS"   "/PDAT"   ",/$,"    "/APPR"   "/ADJA"   "/NN"     ",/$;"    "/APPR"  
# [25] "/APPR"   "/CARD"   "/NN"     "/ART"    "./$:"   

用正则表达式的话来说:“找到以零个或多个标点符号开头的东西,后跟一个斜线,后跟一个或多个字母或标点符号。如果你想包含数字,请切换到[:alnum:].


根据评论,如果您只想要大写字母,则正则表达式将变为:

regmatches(text, gregexpr('[[:punct:]]*/[[:upper:][:punct:]]*', text))

正如@eddi 所建议的那样,[A-Z]并且[:upper:]大致相同。正如@eddi 建议的那样,这个正则表达式将捕获 /LETTERS 案例以及 /$punct 案例:

/[A-Z]+|[[:punct:]]/\\$[[:punct:]]
于 2013-09-13T14:33:13.530 回答