匹配时,一个单词优先于另一个单词吗?

I have the following list.

ID    AllStatuses
1001  {failed|processing|success}
1002  {failed}
1003  {success|failed}
1004  {processing|success}
1005  {failed|processing}

My requirement is to display the most optimistic status alone. Like so

ID    Best Status
1001  success
1002  failed
1003  success
1004  success
1005  processing

Is there a way I can do this with one regex query rather than say check for each one in order and return where i'd have a worst case scenario of three regex checks for statuses with the most optimistic status in the end?

Regex: \{.*(success).*|\{.*(processing).*|\{.*(failed).* Substitution: $1$2$3

Details:

  • .* matches any character zero or more times
  • () Capturing group
  • | Or

Go code:

var re = regexp.MustCompile(`\{.*(success).*|\{.*(processing).*|\{.*(failed).*`)
s := re.ReplaceAllString(sample, `$1$2$3`)

Output:

ID    AllStatuses
1001  success
1002  failed
1003  success
1004  success
1005  processing

Code demo

(\d+)\s+{.*(success|processing|failed).*}

Then take the match from group 1: ID group 2: status

You can make it with one regex, but with additional checks of needed elements at the end in order you need this time.

It is not so short, but I am sure that this is more stable, especially if there will be some changes in algorithm.

Example in javascript, but I am sure, you can easily implement idea in your code

var obResults = {};
var obStrings = {
  1001:  "{failed|processing|success}",
  1002:  "{failed}",
  1003:  "{success|failed}",
  1004:  "{processing|success}",
  1005:  "{failed|processing}",
};

for (var key in obStrings) {
  var stringToCheck = obStrings[key];
  var resultString = "";
  var arMathces = stringToCheck.match( /(failed|processing|success)/ig );
  
  if (arMathces.indexOf("success") != -1) {  
    resultString = "success";
  } else if (arMathces.indexOf("processing") != -1) {  
    resultString = "processing";
  } else if (arMathces.indexOf("failed") != -1) {  
    resultString = "failed";
  }
  
  obResults[key] = { result:resultString, check:stringToCheck };
}
console.log(obResults);

</div>