parsed.org

Tips by tag: regex

Match a MAC Address by xinu on Jun 14, 2008 08:28 PM

Regex to match a colon or hyphen delimited MAC address:

\A                                   # beginning of line
  (?:                                # start cluster first 5 bytes
    [a-f0-9]{2}[:] | [a-f0-9]{2}[-]  # colons or hyphens
  ){5}                               # end cluster first 5 bytes
  [a-f0-9]{2}                        # final byte
\z                                   # end of string
colonhyphenmacpcreregex
Python url regular explession by trick on Sep 19, 2008 10:57 PM

The following Python code is a regex that should fine any valid link. It does not, however, include punctuation at the end of the url:

SCHEMES = ('http', 'https', 'ftp', 'mailto', 'news', 'gopher',
'nntp', 'telnet', 'wais', 'prospero', 'aim', 'webcal')
# Note: fragment id is uchar | reserved, see rfc 1738 page 19
# %% for % because of string formating
# puncuation = ? , ; . : !
# if punctuation is at the end, then don't include it
URL_FORMAT = (r'(?<!\w)((?:%s):' # protocol + :
    '/*(?!/)(?:' # get any starting /'s
    '[\w$\+\*@&=\-/]' # reserved | unreserved
    '|%%[a-fA-F0-9]{2}' # escape
    '|[\?\.:\(\),;!\'](?!(?:\s|$))' # punctuation
    '|(?:(?<=[^/:]{2})#)' # fragment id
    '){2,}' # at least two characters in the main url part
    ')') % ('|'.join(SCHEMES),)

Code taken from the remark markup library.

pythonregexurl
Vim Uppercase by xinu on Jan 22, 2008 09:06 AM

To take a series of lines, say from the cursor to the bottom of the file and make them uppercase, the following would be useful:

%,$s/\(.*\)/\U\1/
regexswapuppercasevivim
RSS