提取"/"之前和之后的字符 [英] Extract character before and after "/"

查看:120
本文介绍了提取"/"之前和之后的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提取"/"之前和之后的字符,但均未成功.句子是:

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 ]+") 

gregexpr 支持.


为了完整起见,您可以不使用正则表达式来执行此操作:用'-'拆分输入,找到包含'/'的零件,修剪.这可能也比正则表达式快.

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 / SP - CEP: 00000-000

Output should be

SAO JOSE DOS CAMPOS / SP

I'm trying str_extract(str, "- [a-zA-Z]{1,} / [a-zA-Z]{1,}") but it's just bringing me

CAMPOS / SP

解决方案

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.

这篇关于提取"/"之前和之后的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆