Reputation: 111
I am trying to retrieve a weapon classname from a string.
The string could look like:
econ/default_generated/weapon_m4a1_silencer_am_m4a1-s_alloy_orange_medium
And I would want:
weapon_m4a1_silencer
But the trick here is, Sometimes the classname could have either 2 or 3 instances of "_"
So a 2nd example would be:
econ/default_generated/weapon_deagle_am_scales_bravo_medium
And would give me:
weapon_deagle
A pattern which could be used is that their is always 2 letters which are inside the _ that come after the classname, (In this case "am")
Any help is much appreciated.
Edit there appears to be cases where there is more _ instances than I thought. Example: https://regex101.com/r/Cmup26/1 weapon_knife_m9_bayonet is not captured.
Upvotes: 1
Views: 73
Reputation:
Updated - to get up until last _AA_
weapon(?:(?:_[^\W_]+)+(?=_[^\W_]{2}_)|(?:(?!_[^\W_]{2}_)_[^\W_]+)+)
https://regex101.com/r/r6yORE/1
where [^\W_]
is [a-zA-Z0-9]
(substitute allowed letters)
Expanded
weapon
(?: # Cluster - requires a _Segment
(?: _ [^\W_]+ )+
(?= _ [^\W_]{2} _ ) # Stop before last _AA_ (high priority)
| # or,
(?: # Stop before first _AA_ (low priority)
(?! _ [^\W_]{2} _ ) # (note- this is only place where
_ [^\W_]+ # segments with NO trailing _AA_
)+ # will match)
)
Upvotes: 1