Extract character before and after “/”

后端 未结 1 1025
长发绾君心
长发绾君心 2021-01-27 10:53

I\'m trying to extract character before and after \"/\" with no success. Sentences are:

XXXX YYY ZZZ - AV HAHEHRS, 3061 - SDDW ASDA DDSF - SAO JOSE DOS CAMPOS /          


        
1条回答
  •  深忆病人
    2021-01-27 11:03

    In your regex there is the space missing. Try:

    str_extract(str, "- [a-zA-Z ]+ / [a-zA-Z ]+") 
    

    Note the space in the character class. Also, {1,} is the long form of +.

    The match will be "- SAO JOSE DOS CAMPOS / SP - CEP". You must get rid of the - in a second step, or use a zero-width look-behind:

    str_extract(str, "(?<=- )[a-zA-Z ]+ / [a-zA-Z ]+") 
    

    Look-behinds are supported by gregexpr.


    For the sake of completeness, you could do this without regex: Split the input by '-', find the part that contains '/', trim. This might be faster than regex, too.

    0 讨论(0)
提交回复
热议问题