I need to capture the following three things into variables from a monster, represented by a string of text, on a game I am playing in one regular expression:
- Monster name (always there. May be ONE or MORE words and MAY include parenthesis)
- Health (optional, ALWAYS enclosed in brackets [] if there)
- Aggression (optional. ALWAYS shows as "attacking you!" IF present)
Examples, followed followed by the name/health/aggression match I desire below each:
EXAMPLE 1: [mob] Crystal Joe [wounded] attacking you!
- Name: Crystal Joe
- Health: wounded
- Aggression: attacking you!
EXAMPLE 2: [mob] Crystal Joe.
- Name: Crystal Joe
- Health: (blank)
- Aggression: (blank)
EXAMPLE 3: [mob] Crystal Joe [scratched].
- Name: Crystal Joe
- Health: scratched
- Aggression: (blank)
EXAMPLE 4: [mob] Joe attacking you!
- Name: Joe
- Health: (blank)
- Aggression: attacking you!
EXAMPLE 5: [mob] A giant red dragon attacking you!
- Name: A giant red dragon
- Health: (blank)
- Aggression: attacking you!
EXAMPLE 6: [mob] A giant red dragon (sparkling) [dying] attacking you!
- Name: A giant red dragon (sparkling)
- Health: dying
- Aggression: attacking you!
I am not a regular expression expert and honestly not well-versed in it. I created the following expression:
\[mob\]\s([\w\s]*)(\[\w+\]\s?)?(attacking you)?
The above seems to work when all three are present, but when the health (i.e. "[scratched]" is missing the Name matches both the name AND the "attacking you!" as the Name (i.e. "Joe attacking you!" which is not what I want. Note that the above expression doesn't attempt to do Example 6 with the parenthesis.
Thanks for any help you can provide.