RegExp: Matching Balanced Parenthesis and Quotes (greedy, non-recursive)

Solution

I need to match all the text within balanced parens, single quotes, or double quotes, but don’t snag on the content within, i.e. be greedy! “Balanced” means there should be a corresponding single or double quote, but not mix them, or there should be a closing paren for each opening paren.

PCRE:

The match will be in capturing group #3.

In short, this regular expression uses a positive lookahead (?=...)  within the lookaround conditional  (?...)  and the backreference \2  to match the initiating character. If it is a single quote or double quote, it was contained within \2 , but if it was an open paren ( , it is not contained in a capture group and therefore it should match a close paren. In retrospect, this could be enhanced, and also needs some tweaks for JavaScript compatibility since lookaround conditionals are not supported.

Do note that this matches the beginning and ending of the string ^$ , so may require tweaking for other applications.

Test Cases

regex101 link: https://regex101.com/r/fG4rZ8/3

Matches:

These will match hello , hello world , or in some cases 'hello world  for the last test case.

Doesn’t Match:

Internet Calendaring (iCalendar): Repeating Events dot ics

Premise

So Sysadmin Day is on the last Friday in July every year. I wanted to add this to a Google Calendar (GUI), but for the year 2014 (July 25) it would always mark it as the fourth Friday. This was problematic since July 2015 has five (5) Fridays. By the way, it worked just fine if I started the event in July 2015.

After some initial searching, I ran across a few resources with hints, but these were focused on the last (week)day of the year:

I decided to roll up my sleeves and give it a whirl in ics format. I started to read up on RFC 2445 “Internet Calendaring and Scheduling Core Object Specification (iCalendar)” over at IETF. After realizing it was 148 pages long I decided to create a working event in the calendar, export it, extract and modify the event, then import the event back into the calendar. This worked.

Source & How-To

And now the ICS source for System Administrator Appreciate Day:

Source Explanation

  • BEGIN/END VCALENDAR: Entire calendar encapsulation. (Hmm, I wonder if multiple calendars will work? Need to read the spec.)
  • BEGIN/END VEVENT: Single event encapsulation within the specific calendar.
  • DTSTART: The start date of the event; this is an all-day event so there is no time nor timezone.
  • DTEND: Like  DTSTART, only for when the event will end – this is the next day for an all-day event.
  • RRULE: This is the bread’n’butter. The frequency is monthly every 12 months (so once per year) on the last (-1) Friday (FR);   1FR would be the first Friday,   2FR would be the second,   -2FR would the second-to-last Friday, etc. See §4.3.10 “Recurrence Rule” on page 40 of RFC 2445 to start.
  • SUMMARY: The formal name (title) of the event.

Make It & Import It

  1. Create a new plain text file (after all, ICS/iCalendar format is just text/calendar!)
  2. Copy and paste from   BEGIN:VCALENDAR to   END:VCALENDAR.
  3. Save it as whatever you want, in this case I’ll call it   sysadminday.ics.
  4. Import to an existing Google Calendar.
  5. ????
  6. EAT:CAKE!!!!

“Falsy” vs “Falsey” – I’m Making the Call

This is probably the most asinine blog post I’ll write, but for my sanity, it must be done.

It is spelled falsy.

Without the “e” between the “s” and the “y” – priod, th nd.

Okay. So what is Falsy anyway?

In loose-typed languages like PHP or JavaScript values can be considered Boolean false if they are '' (empty strings), the number 0 (zero), NULL, and most obviously Boolean false itself, just to name a few (in PHP an empty array is considered false, while in JavaScript it is not). It is important to note that this is only in the context of non-strict comparison, i.e. == and not  ===, and when type casting to Boolean (see PHP’s type juggling reference).

When describing a false-like value it is easier/faster to type “falsy” (or the now incorrect way of “falsey”) since it has less keystrokes, and it is easier to read due to the lack of character bloat; incidentally it also fits nicely as a variable because there is no hyphen (devil’s advocate: camelCase) and it is not a reserved word. It is important that the developer does not simply describe the value as “false” since this implies a variable of type Boolean, however using “falsy” implies a false-like value of any data type which, albeit more ambiguous in its definition, is more descriptive in its context.

Supporting Data

Let’s let the data speak for a moment. After analyzing a Google Trends chart of “falsy” vs “falsey,” the results are initially a bit misleading. “Falsey” is the clear winner at first glance, only to find that there is a name and published articles with the name “Falsey.” Considering that “falsy” is not trailing by many datapoints provides the confidence that it is in use and very applicable. Most importantly in regards to the related searches, “falsy” prevails over “falsey” which does “not [have] enough search volume to show results”: “javascript falsy” and “falsy values.”

Addition: Douglas Crockford even uses “falsy” in his writing, “The Elements of JavaScript Style.” Perhaps I didn’t need to write this post after all, but I sure hope it solidifies the answer indefinitely and provides a resource for any other concerned developers out there.

Spelling Review

I am by no means an English major, or anywhere near one, but I do have a knack for research and comprehension.

With regard to the suffixation of the letter “y” (vowel) when transforming a word, the “e” is dropped per the rules:

In the word “false,” the word ends with the vowel “e,” is preceded by the consonant “s,” and none of the exceptions are applicable.

F – A – L – S – Y

Playing Around with PHP Namespaces, Classes, and Variable Instantiation

Just needed to do some sanity checks, figure out some syntactical sugar (i.e. tricks), and test certain scenarios. I figured I’d share my findings with the world. Explanations are very light (brain dump) and embedded in the code; output follows directly. Oh, and this is a single stand-alone file.

php://stdin

Well, this really isn’t php://stdin, but the headline looks cool.

php://stdout

Yes, this time it really is stdout:

 

ECMA-262: Breaking Out of Nested Loop Control Structures

PHP: Optional Numeric Argument for break

My first dynamic language was PHP and with that came the break statement which ends the execution of a control structure (e.g. for, while or case). It also “accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.” This optional argument is beneficial when there are nested loops that need to be terminated prematurely without adding extra logic and without halting the entire script execution (e.g. exit).

In the following example the second/nested for loop will be terminated during the first iteration, but the outer-most loop will execute tens times.

…but in the next example it will break out of both for loops resulting in $i never incrementing from 0 to 1.

JS/AS: LabelledStatement (TL;DR)

What about ECMAScript Languages (ECMA-262)? I struggled with the absence of this feature for languages based on the ECMA-262 standard like JavaScript and ActionScript. These languages (as far as I knew) have the break statement which works just like it does in PHP, but without the optional numeric argument. This made stopping nested control structures a PITA.

Well, maybe if I RTFM I would’ve been acquainted with the LabelledStatement Statement and saved myself the heartache. This can be observed in section 12 of the ECMA-262 Langauge Specification, Edition 5.1. Basically the syntax goes something like this:

Note “theLabelName” on lines 1 and 3. This is the bread’n’butter. All that needs to happen is to label the control structure using the standard variable naming convention and post-fix it with a semicolon (:). This becomes the “calling card” of that specific construct. Lastly, the break statement has an optional argument where the previously established label can be applied.

Further Reading

See ECMA-262 5.1 Edition ECMAScript Language Specification, §12, p86.

Explode RGB Unsigned Integer into Individual R, G and B Values + RGBA

At irregular intervals I need to take a RGB uint – e.g. 0 is rgb(0,0,0) and 16711680 is red (#ff0000) and convert it to an unsigned integer and visa-versa.
I need this from time-to-time and always forget. That, and somehow I misplace the code or leave it behind with my employer. I will now forget no more thanks to “Splitting an RGB uint into its separate R G B components” question on stackoverflow and this post.

RGB

Here’s the run down (in AS3)…

Convert RGB to uint (decimal):

Convert uint (decimal) to RGB:

RGBA

So after implementing some of this, I thought… "what about alpha channel support?" Well keep in mind, alpha is a graceful implementation/add-on and does not obstruct (or shift) the original data of the RGB value.

With this in mind, note that alpha is in the upper bit range (24); in hexadecimal format that is #AARRGGBB. It is doubly important to make sure that an unsigned integer is being used. With just normal RGB you can get away with a signed integer (−2,147,483,648 to 2,147,483,647) since you don’t need higher values, but you need unsigned for RGBA (0 to 4,294,967,295). For example, #FFFFFF for RGB merely equates to 16,777,215 in decimal format which is well under the 32-bit signed integer maximum (one-twenty-eight to be exact, think about it), however #FFFFFFFF for RGBA equates to 4,294,967,295 which is the exact maximum of a signed 32-bit integer (e.g. uint.MAX_VALUE).

Moral of the story: don’t use datatypes of “Number” or “int,” just “uint.”

Anyways, here’r the goods, going from uint (decimal) to RGBA (errr, ARGB?):

And going from RGBA to uint (decimal, base 10):

Further Reading

Adobe Shadow

This is probably the greatest thing since sliced bread: Adobe Shadow (Sd).

In summary, it synchronizes your desktop (PC and Mac) browser with your hand-held touch device (both Android and iOS) for streamlined mobile design, development, and troubleshooting. You can get up and running in 3 easy steps:

  1. Download Adobe Shadow from Adobe Labs
  2. Download the app on your mobile device
  3. Download the Google Chrome Browser extension

I haven’t used it yet, but will give it a whirl within the next few days… and probably post my experience.

RHEL + SELinux: Managing Confined Services

What this post is really about: http://linux.web.cern.ch/linux/scientific6/docs/rhel/Managing_Confined_Services/

Found this gem of a guide when it comes to managing SELinux. This page is a mile-n-a-half long, but a great read when you’ve got nothing to do but research linux security policies. I stumbled across it when searching for the section #sect-Managing_Confined_Services-The_Apache_HTTP_Server-The_Apache_HTTP_Server_and_SELinux (i think the docs are taking SEO to a whole new, overkill, level – or perhaps its just scripts “do what it do”). This doc has an awesome intro to custom selinux policy generation.

What this post ended up being about (the title is completely misleading):

I was configuring a utility server with a “utilwww” user (needed to test some geoip scripts, using maxmind db api,  on a boat load of IP addresses). For easier access i wanted to grant FTP access using vsftpd – crap, in retrospect I could have just used SCP, oh well – but was having some problems with selinux blocking the reads on the dir. This was expected since the type wasn’t set to httpd_sys_content_t (the VM is running centos 5.7 btw).

Another problem was that by default the home dir’s mode was 700. Not sure why useradd does this and not 744 (or it might have been 744, don’t recall exactly). After scratching my head a bit more wondering why the sucker wasn’t working, i realized that apache (httpd) needs execute perms as well, so a little chmod 755 utilwww did the trick.

In summary (might be missing a few steps here, but this is at least the high level):

puttin’ stuff down.

putting tips and tricks here that are in my head. at least WP will retain the information, rather than FB… sometimes I ask myself how the hell I did something and go to search for it: not in my browser history, not in old fb posts (plus there’s too much other crap to sift through), and I don’t post to anything else.
…also rolled this thing into twitter, maybe it’ll help someone.