Ruby Regex matching unexpected characters -
i trying write script parses filename of comicbook , tries extract info such seriesname, publication year etc.in case, trying extract publication year name. consider following name, need match , value 2003. below expression had this.
r = %r{ (?i)(^|[,\s-_])v(\d{4})($|[,\s-_]) }
however matches number irrespective of character have before v or after number
i expect first 2 not match , third match.
- 010 - star batman & robin boy wonder 01 - av2003
- 010 - star batman & robin boy wonder 01 - v2003t
- 010 - star batman & robin boy wonder 01 - v2003
what doing wrong in case?
inside character classes (ie. []
s) -
character has special meaning when it's between 2 other characters: creates range starting character before , ending @ character after.
here, want literally, should either escape -
or (more idiomatically in regex) put first or last character in []
.
also, btw, have literal space characters, no /x
modifier, don't want capture what's before , after year, final pattern be:
%r{(?i)(?:^|[,\s_-])v(\d{4})(?:$|[,\s_-])}
Comments
Post a Comment