提取"/"之前和之后的字符 [英] Extract character before and after "/"
问题描述
我试图提取"/"之前和之后的字符,但均未成功.句子是:
XXXX YYY ZZZ - AV HAHEHRS, 3061 - SDDW ASDA DDSF - SAO JOSE DOS CAMPOS / SP - CEP: 00000-000
输出应为
SAO JOSE DOS CAMPOS / SP
我正在尝试str_extract(str, "- [a-zA-Z]{1,} / [a-zA-Z]{1,}")
,但这只是带给我
CAMPOS / SP
正则表达式中缺少空格.试试:
str_extract(str, "- [a-zA-Z ]+ / [a-zA-Z ]+")
请注意字符类中的空格.另外,{1,}
是+
的长格式.
匹配为"- SAO JOSE DOS CAMPOS / SP - CEP"
.您必须在第二步中摆脱-
,或使用零宽度的后向:
str_extract(str, "(?<=- )[a-zA-Z ]+ / [a-zA-Z ]+")
为了完整起见,您可以不使用正则表达式来执行此操作:用 I'm trying to extract character before and after "/" with no success. Sentences are: Output should be I'm trying
In your regex there is the space missing. Try: Note the space in the character class. Also, The match will be Look-behinds are supported by For the sake of completeness, you could do this without regex: Split the input by 这篇关于提取"/"之前和之后的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!gregexpr
支持.
'-'
拆分输入,找到包含'/'
的零件,修剪.这可能也比正则表达式快.XXXX YYY ZZZ - AV HAHEHRS, 3061 - SDDW ASDA DDSF - SAO JOSE DOS CAMPOS / SP - CEP: 00000-000
SAO JOSE DOS CAMPOS / SP
str_extract(str, "- [a-zA-Z]{1,} / [a-zA-Z]{1,}")
but it's just bringing me CAMPOS / SP
str_extract(str, "- [a-zA-Z ]+ / [a-zA-Z ]+")
{1,}
is the long form of +
."- 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 ]+")
gregexpr
.
'-'
, find the part that contains '/'
, trim. This might be faster than regex, too.