How to do "or" but not commit "or" part in regex?

I am looking for a regex that can match date strings. I'm having trouble specifying the expression "or": /\d{4}-\d{2}-(XX|\d{2})/ does not work, because it returns only part of XX:

 "2007-01-XX 2008-01-01".scan(/\d{4}-\d{2}-XX/) ["2007-01-XX"] "2007-01-XX 2008-01-01".scan(/\d{4}-\d{2}-\d{2}/) ["2008-01-01"] 
+4
source share
2 answers

Use a non-capturing form group (?:...) instead of a form capturing group (...) . So:

 "2007-01-XX 2008-01-01".scan(/\d{4}-\d{2}-(?:XX|\d{2})/) 
+7
source
 "2007-01-XX 2008-01-01".scan(/(\d{4}-\d{2}-(XX|\d{2}))/).map(&:first) 
0
source

Source: https://habr.com/ru/post/1401221/


All Articles