简体   繁体   中英

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 .

From this, I want to fetch @_sfdc.Account.phonenumber.value and @_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;

/@_[\\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

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 .

Also you say that you want to match both the @_sfdc and @_hrms prefixes, so let's change that. There are two ways to do this:

  • looking specifically for @_sfdc and @_hrms : /@_(sfdc|hrms)\\.([a-zA-Z\\.])+/g
  • or matching any string that starts with @_ : /@_([a-zA-Z\\.])+/g . In this case, hrms. and sfdc. are captured by the same block that captures the rest of the string ( [a-zA-Z\\.]+ )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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