I am writing a regular expression for parsing PHP annotations in a flexible way. The thing is that I am getting whitespace at the end of the matches and I don't know how to get rid of it. Any improvements to the expression are also welcome. Thanks /[\*\s]*@(?P<name>\w+[\\\w]*?)\s? (?P<value> (?: [\w\s\"\<\>\_\#\=\-\.\'\{\}:;,\*\(\)\[\]]*[^\R\*\s\/] ) (?:\s | $) )/gsxmu Demo: https://regex101.com/r/zU1eQ5/5