简体   繁体   English

用于从父字符串中识别和提取两个不同字符串的正则表达式

[英]regular expression to identify and extract two different strings from parent string

I have the following string: your lead count is @_sfdc.Account.phonenumber.value, you are having @_hrms.leaves.leavescount per year . 我有以下字符串: your lead count is @_sfdc.Account.phonenumber.value, you are having @_hrms.leaves.leavescount per year

From this, I want to fetch @_sfdc.Account.phonenumber.value and @_hrms.leaves.leavescount . 从这个,我想获取@_sfdc.Account.phonenumber.value@_hrms.leaves.leavescount It should fetch all dot separated stings even if we have more than one like in this case. 在这种情况下,即使我们有多个点,它也应该获取所有点分隔的字符串。

Currently I have tried with this regex: /@_sfdc.([a-zA-Z]).+/g; 目前,我已经尝试使用此正则表达式:/ /@_sfdc.([a-zA-Z]).+/g; / /@_sfdc.([a-zA-Z]).+/g;

/@_[\\w\\.]+/g

这与“ @_”匹配,后跟字母,数字或点( . )的任何序列。

You were on the right path, almost there! 您在正确的道路上,几乎在那儿! The main error here is that . 这里的主要错误是. is a reserved character in a regex which means "any character". 是正则表达式中的保留字符,表示“任何字符”。 To actually match a dot character, you need to escape it with a backslash: \\. 要实际匹配点字符,您需要使用反斜杠将其转义: \\.

So, your regex becomes /@_sfdc\\.([a-zA-Z])\\.+/g 因此,您的正则表达式变为/@_sfdc\\.([a-zA-Z])\\.+/g

Another thing to notice is that you want to match letters and dots in the same group, while your current expression requires the match to end with a dot. 需要注意的另一件事是,您要匹配同一组中的字母和点,而当前表达式则要求匹配以点结尾。 So, let's move that dot inside the matching group square brackets: /@_sfdc\\.([a-zA-Z\\.])+/g . 因此,让我们在匹配的组方括号内移动该点:/ /@_sfdc\\.([a-zA-Z\\.])+/g .([ /@_sfdc\\.([a-zA-Z\\.])+/g .])+/ /@_sfdc\\.([a-zA-Z\\.])+/g

Also you say that you want to match both the @_sfdc and @_hrms prefixes, so let's change that. 另外,您说您想同时匹配@_sfdc@_hrms前缀,所以让我们对其进行更改。 There are two ways to do this: 有两种方法可以做到这一点:

  • looking specifically for @_sfdc and @_hrms : /@_(sfdc|hrms)\\.([a-zA-Z\\.])+/g 专门为@_sfdc@_hrms寻找:/ /@_(sfdc|hrms)\\.([a-zA-Z\\.])+/g @_sfdc ( @_hrms .([[ /@_(sfdc|hrms)\\.([a-zA-Z\\.])+/g @_hrms / /@_(sfdc|hrms)\\.([a-zA-Z\\.])+/g
  • or matching any string that starts with @_ : /@_([a-zA-Z\\.])+/g . 或匹配任何以@_开头的字符串:/ /@_([a-zA-Z\\.])+/g .])+/ /@_([a-zA-Z\\.])+/g In this case, hrms. 在这种情况下, hrms. and sfdc. sfdc. are captured by the same block that captures the rest of the string ( [a-zA-Z\\.]+ ) 由捕获字符串其余部分的同一块( [a-zA-Z\\.]+ )捕获

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM