I have this string:
"/--shirt-blue-/2346985"
I need to detect '/--' I solve using:
\/-{2}
But I need to detect if the string contains '/--' and the final contains '-/digit'
/--anything-/digit
How can I do that? Thanks.
CodePudding user response:
You can use
/\/--(.*?)-\/\d \z/
See the Rubular demo.
Details:
\/--
- a/--
string(.*?)
- Group 1: any zero or more chars other than line break chars as few as possible-\/
- a-/
string\d
- one or more digits\z
- end of string.
In Ruby,
puts "/--shirt-blue-/2346985"[/\/--(.*?)-\/\d \z/, 1]
shows shirt-blue
.
CodePudding user response:
With your shown samples and attempts please try following regex:
^\/--(?:[^-]*-){2}\/\d $
Here is the Online demo for above used regex in Ruby.
Explanation: Adding detailed explanation for above shown regex.
^\/-- ##Matching starting /(escaped it) followed by 2 dashes.
(?:[^-]*-){2} ##In a non-capturing group matching everything till 1st occurrence of - including - and this match 2 times.
##So basically this will match till 2 dashes.
\/\d $ ##Matching literal / followed by 1 or more digits at the end of the value.
CodePudding user response:
If there should be at least 1 char other than - in between:
^\/--(?:[^-] -) \/\d \z
Explanation
^
Start of string\/
Match/
--
Match literally(?:[^-] -)
Repeat 1 times matching any char except-
and then match-
\/\d
Match/
and 1 digits\z
End of string
See a regex 101 demo and a Rubular demo