Luis Henrique
Luis Henrique

Reputation: 771

Capture multivalues with regex

I'm trying to create a regex that always captures myValue (this name is figurative, it can change, it's just to know the position of the value), but in some cases where unexpected characters enter like -, my regex fails, any suggestions?

regex:

.+-|(.*)(:)

sample:

abc:abe-myValue-ora:RZ
abe-myValue:mySAP
web543-p:abd-myValue-p:RZ
VM:afd-myValue.sl:ESX
VM:EUA-myValue:ESX
myValue:98
abc-myValue:98

regex101

Upvotes: 1

Views: 30

Answers (1)

anubhava
anubhava

Reputation: 785276

Since you have not specified all the rules of this match, here is a possible solution based on your examples:

^(?:.*?-)?([^:.-]{2,})[:.-]

Updated RegEx Demo

RegEx Details:

  • ^: Start
  • (?:.*?-)?: Match 0 or more of any character followed by - in an optional match
  • ([^:.-]{2,}): Match 2 or more of any character that are not included
  • [:.-]: Match : or . or -

Upvotes: 3

Related Questions