Search code examples
regexextracttext-extractionuidpasswd

extract UID from /etc/passwd using RegEx


I would like to extract UID from /etc/passwd file which looks like this-

www-data:x:33:33:www-data:/var/www:/bin/sh

The problem I'm facing is that even if I match first 33, the second 33 (GID) is also getting matched. How I match first occurrence only?

So far I've this- [\d]+


Solution

  • You need to use a possessive-quantifier:

    Try

    [\d]+?