Regex: matching either of 2 fields, in any order
I have a string that I'm searching, with sub-fields that are delimited by '~'. I'm particularly interested in 2 of the fields, either of which may or may not exist, in an unspecified order, and are identified by an initial 'C' and 'O' respectively. The 'C' is always followed by one or more digits, the 'O' is followed by one or more characters. For example, these are all valid. ~A123~C456~MA4 ~C12345~OA1~A123 ~A4~MH1 I'd like to see if I can use regular expressions to say, "If there is a 'C' field, replace the digits after it with these digits. If there is an 'O' field, replace the characters after it with these characters." I can do this quite easily with two different regex search-and-replaces; if the fields were always in the same order, I'd have no problem with that, either. Is it possible to do this in a single operation, given that the order of the fields is arbitrary? ------------------------------ John Wismar BSA - AllData Offline Products john.wismar@alldata.com
I have a string that I'm searching, with sub-fields that are delimited by '~'. I'm particularly interested in 2 of the fields, either of which may or may not exist, in an unspecified order, and are identified by an initial 'C' and 'O' respectively. The 'C' is always followed by one or more digits, the 'O' is followed by one or more characters.
For example, these are all valid.
~A123~C456~MA4 ~C12345~OA1~A123 ~A4~MH1
I'd like to see if I can use regular expressions to say, "If there is a 'C' field, replace the digits after it with these digits. If there is an 'O' field, replace the characters after it with these characters."
I can do this quite easily with two different regex search-and-replaces; if the fields were always in the same order, I'd have no problem with that, either. Is it possible to do this in a single operation, given that the order of the fields is arbitrary?
Yes do a conditional search and replace: search for "C(\\d+)|O(\\w+)" and replace with "(?1first-text)(?2second-text)" don't forget to set the flag format_all when calling regex_merge to do this (in future conditional replacements will only be enabled when actually requested). John.
participants (2)
-
John Maddock
-
john.wismar@alldata.com