Skip to main content
Became Hot Network Question
edited tags
Link
user13892
  • 9.5k
  • 2
  • 14
  • 41
Source Link
user13892
  • 9.5k
  • 2
  • 14
  • 41

How to write a rule that matches alternatives but return one of them always?

Consider the following rule:

$rule={"Period ID"|p:"Period":>p};

This rule will match "Period ID" and "Period". When it matches "Period" it returns "Period" but when it matches "Period ID" it returns Sequence[]. I want it to return "Period" in both cases.

{"Period ID","ID","Period"}/.$rule

{"ID", "Period"}

Within the $rule I don't want to duplicate the string. For example, the following $rule would work:

$rule={"Period ID"|"Period"->"Period"};

and returns the desired:

{"Period", "ID", "Period"}

But the $rule has "Period" appearing twice. How can I do this using RuleDelayed?