You're viewing all posts tagged with tricks

Greedy and Reluctant Qualifiers

A common gotcha when using regular expressions occurs when using the default (greedy) qualifiers + ? and *. These qualifiers will attempt to make the longest match they possibly can.

The regular expression:

/'(.*)'/

will successfully match and group the word there in hello 'there', but will actually run on to the last single quote in the string hello 'there' how are 'you', matching there' how are 'you.

One solution is to restrict the set of characters you are searching for with the greedy qualifier, thus ensuring the match will finish before hitting the terminating character:

/'([^']*)'/

This works, but the more readable option is to turn the greedy qualifier into a reluctant one:

/'(.*?)'/

By adding a ? to the qualifier the expression will try to match the minimum string that satisfies the expression.

Making Java Processes Play Nice

Looking around, it seems that there is no easy way to stop Java from eating all your system resources when running a particularly heavy-going task.

Thankfully my lovely colleague Ben made me aware of a helpful UNIX command called nice.

By prefixing nice to any command you can ask the scheduler to be a bit more kind, running the process at a slightly lower priority to ensure it doesn’t starve other resources of CPU time:

nice java ExpensiveTask

Mac OS X: Tar Without Resource Forks & File Metadata

You can stop Tar from archiving the ._ files that Mac OS uses to mark up its file system with an undocumented environment variable:

export COPYFILE_DISABLE=true

Thanks Norman Walsh