Regular Expressions, Part III (Phone Number – basic)

As I promised, here is another discussion about regular expressions, and more specific, phone numbers. The first part of this, I’m going to discuss the HackerRank Python Regex problem on validating phone numbers with regular expressions, but as its a very simplistic case, I’m going to expand on it.

Because, it’s pretty simplistic, I’m simply going to show you the code for the Hackerrank problem.

^[789]\d{9}$

Since it’s so simplistic, I figure I’ll just go over the whole thing. First, notice the ^ and $ anchors. Again, these refer to the fact that everything is occurring on the evaluation. I’ll explain this better last…

The next piece is [789]. This simply tells us that we are looking for a 7, 8, or 9 as the first digit.

Next is \d{9} which is 2 parts, the first \d meaning we are looking for any digit, and the second {9} meaning we are looking for exactly 9 of them.

All that combined, along with the anchors I mentioned before, means that we are searching the input we are using regex on for 10 numbers, starting with a 7, 8, or 9. Nothing else will generate a match.

Now, suppose we didn’t care about starting with a 7, 8, or 9. The first thing we would do is drop the [789] and change {9} to {10}. Simple enough!

However, lets make this a bit more realistic. This makes no allusion to formatting… it doesn’t quantify the real formatting for a North American phone number. So let’s sat that formatting (which I’m simply going to refer to North American Phone Numbers as phone numbers).

A little education for us here. Phone numbers are divided into 3 sections. The first 3 digits are referred to as the area code. The next 3 digits are referred to as the Central Office code, frequently called the exchange, and then the last 4 digits are the subscriber number. Now there are some rules for the area code and the exchange.

For both the area code and exchange, the first digit must be between 2 and 9 (thus no 0’s or 1’s). The next 2 digits can be anything, however, for the exchange, they can’t be 1 and 1 (as those are for special services such as 911 or 411, etc). For simplicity, I’m going to leave it at that, and not worry about the extra formatting or extensions and such… this is more for a basic understanding…

We know we will use our anchors still, so ^$ go in immediately. Also, the last 4 digits are simple as well. I’m going to use parenthesis in the regex to simply group everything, so we know which sections are for which. We know that \d{4} will work for our last for digits, so we’ll add those.

So we currently have ^(\d{4})$. Now, the area code is pretty simple. It’s 3 digits, but can’t be 0 or 1, so that means [2-9]\d{2}, so we’ll add that in the front, giving us ^([2-9]\d{2})(\d{4})$. Now comes the hard part.

The exchange is a bit more difficult, because it can’t be #11. It can be #1#, or ##1, so it has to be broken down into multiple pieces.
The first piece is if the 2nd digit is a 1. That would be [2-9]1[02-9]. Remember the first digit can’t be 0 or 1 (always, from the above rule). We already stated that in this piece, the second digit is 1, which means the third piece can’t be 1, thus the [02-9].
Now the next piece will be the 3rd digit being 1. The nice thing is all the other rules apply, so all we need to do is switch the 2nd and 3rd numbers, giving us [2-9][02-9]1.
Now the last piece is the one that makes logical sense. If we’ve covered the case where the 2nd digit is 1, and the case where the 3rd digit is 1, then all we need to do is cover the case where neither digit is 1. We already have that rule [02-9], so all we do is put that in both spots, giving us [2-9][02-9][02-9]. There are all 3 pieces. Now we just put them together.

If you remember back in the Roman Numerals post, we talked about alternation, or the pipe |. That is basically a way to say this or that, and would look like this|that. All we have to do is combine the 3 pieces, putting that alternation between each piece, giving us [2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9][02-9].

Which gives us our final result of ^([2-9]\d{2})([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9][02-9])(\d{4})$. All done.

For the record, I tested the above on regexr.com, adding the following phone numbers: 5123456789 and 5123114567. It only matched the first one! Yay!

Until next time!

Never B Flat, Sometimes B Sharp, Always B Natural

Regular Expressions, Part II (Roman Numerals)

So, lately with HackerRank, I’ve been going over their regex section in python. I thought I’d review some of what I’ve done, for my own sake to review later on down the road!

Here are some of the regex problems I’ve solved… For the record, I’ve modified the original post of this to what it is now – before, I just listed the regex expression, and what the whole expression did… Now, however, I’m explaining what each bit does for my own understanding! I’m also reducing how many I do per post so there will be more posts about it as I go into a deeper detail for them…

The one that I will be discussing today is a regular expression to validate roman numerals.

For roman numerals, the expression is:
^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$

We start of with ^ which is called an anchor and it is used to say we are starting from the beginning of the string we are comparing…

Next is M{0,3} which tells us we are expecting the letter M between 0 and 3 times. Which covers any thousands digit.

The next piece is (CM|CD|D?C{0,3}) and it involves our first grouping. The parenthesis defines our grouping which can be used to later to pull out this specific information. We next have letters between pipes (|), which is called alternation. It’s saying we are next looking for CM or CD or D?C, which the question mark here means that the D is optional – meaning we are looking for C alone, or DC together, and that is followed by {0,3} which, like before, means we are looking for 0-3 of… so that whole grouping says we are for CM (900), CD (400), or D (500) with up to 3 C’s… which covers hundreds portion of any number over 99.

The piece after this is (XC|XL|L?X{0,3}) and it is also a grouping (which again, we can pull from later). This section covers our 10’s digit, and just like the last grouping, is looking for XC (90) or XL (40) or L (50) and/or up to 3 X’s (10 each).

The last piece is (IX|IV|V?I{0,3})$ and covers our 1’s digit. It’s also grouped so we can pull out this information later. Just like the prior 2 groups, it’s looking for IX (9) or IV (4) or V (5) and/or up to 3 I’s (1 each). The last piece, the $, is referring to the end of the line.

So if we put this all together, we are saying in the line we pass in, if there is a character that isn’t an M, D, C, L, X or I, this will fail. Also, if proper characters aren’t in the proper order, this will fail, and if there is anything after the lowest number, it will fail.

All in all, a pretty neat expression!

Next time, I’ll go over phone numbers.

Never B Flat, Sometimes B Sharp, Always B Natural

JACU II

Once again, it’s another installment of JACU (or Just Another Computing Update)!

HackerRank: I currently have 1577.91 points, and am ranked 793. That means I’ve earned 570 points, and risen 698 ranks! Not bad. I’m almost through regex in python (of which there will be a couple of posts for)

CodeWars: I’m currently at 5kyu with 154 honor points, and am ranked # 10923 [93%]. I’ve increased 1 kyu, and 63 honor, as well as risen 8374 ranks (and 10%). I’m going to work on these a bit more as well.

CheckIO: I’m currently level 9, with 543/750. I completed the last 2 puzzles in Home, as well as finished Elementary, and have started on O’Reilly. There are some puzzles that are locked, since I’m not a paying member… I’ll think about remedying that…

CodeEval: I’m currently at 70 points – but haven’t completed the challenge, as they don’t tell you what is wrong, so I’m not sure where the bug is in my submission (I get the correct answer on my system), so I’ve sort of stalled on this one… I’ll be coming back to it soon.

Project Euler: Using HackerRank’s Project Euler contest, I’m ranked 1307th/48187, so that’s a good increase of 1608 ranks, and about 5000 more contestants joined, so I’m going strong here. I’m currently working on Project #23 (which has been solved on the PE site, just having an issue solving 1 test case, and not getting any help on the details…

So that is where I’m at! I will be blogging about my daily programmer exercises, once they are closed!

The More I Drink – Blake Shelton
Never B Flat, Sometimes B Sharp, Always B Natural

How To: Use Let’s Encrypt with a (my) hosted VPS!

Please Note: The following tutorial is how I used my VPS and installed Let’s Encrypt on all of my sites. This is also for apache. nginx is not too different, but you’ll have to deviate from this tutorial slightly.

First thing to note is there are lot’s of less manual ways to handle this… Sites with newer versions of CPanel have a Let’s Encrypt feature, so you should use that. Also, if you are hosting yourself, or using a box ‘you own’ as in have full admin privileges (such as Digital Ocean), then there are much better ways to do this… However, I purchased a 2 year plan with my VPS, and wanted to do it this way to see how difficult it really is (it really isn’t that bad – lots of copying/pasting).

As I’m doing this for my 4 sites (my ME site, my upcoming business site, my gaming page, and of course this site here! The steps are the same for each, you just have to do it for each site!

So here you go…

  1. You will need to have the following 3 windows open (however you choose)…
    • an SSH session into your hosted space (I just used Terminal and ssh’d into myusername@myarvixeip.tld and entered my password. I know I should set up an SSH Key so I don’t worry about the password, and I’ll probably do that once I reformat my computer). I’ll refer to this as ssh when I’m talking about this window below.
    • a web browser open to your cpanel, which I’ll refer to as cpanel
    • and a web browser open to Let’s Encrypt’s free HTTP Site, which I’ll refer to as encrypt
  2. In cpanel, go to your SSL/TLS Manager and select Private Keys. In there, you will generate 2 private keys (I used 4096 bit). I labeled 1 Account for musicalcoder and the other was domain for musicalcoder. Will make it simpler later on
  3. In ssh, you should see where your keys were created (~/ssl/keys). You’ll then create a symbolic link to the account key (named account.key). This will be useful in future steps.
  4. In ssh, Enter openssl rsa -in account.key -pubout which generates your public key for your private account key. You’ll then copy that into the clipboard.
  5. In encrypt, Enter your account admin email address (for me it was admin@musicalcoder.com), and then paste that public key into the box. You’ll then click the Validate Account Info button.
  6. In cpanel, go to your SSL Manager, and then go to CSRs. Here you will want to generate a CSR with your DOMAIN key for your site (which we generated earlier, remember?) Fill out all the pertinent information it wants, and then click generate. (NOTE:If you want multiple entries, such as yoursite.com and www.yoursite.com, you must enter them both in the domain window, on separate lines.) Once that’s done, you’ll see your ‘Encoded CSR’. You will copy this to your clipboard.
  7. In encrypt, paste your CSR information in step 2, then click Validate CSR
  8. At this point, you should be at Step 3 in encrypt! Here there are 4 boxes that you will want to copy, one at a time. You’ll notice that it references account.key – because we made a symbolic link before, this will be as simple as copy and paste. Without it, you’ll have to edit the command to reference your account key.
  9. Paste the command into your ssh session, and you’ll get a a huge set of hexadecimal values. You’ll copy the entire output (including the (stdin) bit) into your clipboard, and the paste it in the corresponding text box in encrypt. Repeat this for each of the 4 entries.
  10. Once you’ve done all 4, click the Validate Signatures button. This should take you to step 4.
  11. In step 4, you’ll copy and paste another entry similar to step 3. Once you’ve pasted the result, you’ll then click on Option 2 – file based and then how do I do this?. In the section that appears, copy step 4 (the echo command) and paste it in ssh. It will error (as expected) because its using a /path/to/www reference, which you will have to change to your actual www reference. Once you’ve fixed that, run the command.
  12. Click the I’m now serving this file on [yourdomain.tld]. If you entered multiple domains in step 6, you’ll have multiple files to create. They are just repeating the last 2 steps for encrypt step 4.
  13. Once that is done, you’ll be on encrypt step 5, and will generate your certificate. Copy the certificate code.
  14. In cpanel, go to SSL Manager, and then Certificates, and paste that code into the textbox provided. Then click Generate
  15. When that’s finished, you should then have an entry at the top for your domain. Click Install
  16. When that’s finished, go back to encrypt, and test your installation.
  17. Before closing ssh, you can delete the directory .well-known and its subdirectories and files.

All finished!!!

I did that for each of my domains… it took about a half hour, although the last site to only a minute or two as I was used to it.

I hope this proves useful to you!

Never B Flat, Sometimes B Sharp, Always B Natural

September 2016 Infodump (and October as well…)

As I stated, I’m doing monthly updates (at least when I post during the month – which is why this is October as well, since there were NO October posts – shame on me…). Again, as I have had no visitors (that I can tell) and I’m not promoting or selling anything, so there is no income – this will just be a goals update!

My September 2016 Goals were:

  1. 10 blog posts
  2. 8 comments on other blogs

So, let’s see how I did…

So only roughly 60% of my goals were met…

I will work harder on making those goals…

So, for November, I’m not changing anything… My goals are:

  1. 10 blog posts
  2. 8 comments on other blogs

Until next time!
Never B Flat, Sometimes B Sharp, Always B Natural

Where, oh where, has MusicalCoder gone???

The month of October has slipped on by, and me without a single blog post… whatsupwiththat?!?!

I’m going to make the effort (attempt at least) to blog every day this month… we’ll see how that works!

More soon!