Shared posts

13 Jun 13:00

Beginner’s Guide to Regular Expression (Regex)

by Preethi Ranjit

A regular expression is a set of characters forming a pattern that can be searched in a string. Regex can be used for validation such as to validate credit card numbers, for search i.e. via complex text matches, and for replacing matched text with another string. It also has great multiple language support – learn it once and you can use it across many programming languages.

I've seen few people take the first look at regex, and ignore it completely. I don’t blame them; regex's syntax is complex and will make many cringes just like those command-line languages, only worse. But then every new thing is scary and seems impossible to learn at first. So, borrowing Horatius' words I'll say this; Begin, be bold, and venture to be wise.

30 Useful Regular Expressions Tools and Resources

30 Useful Regular Expressions Tools and Resources

Regular expression, or often known as regex, is a pattern that consist of rules used to match a... Read more

Regex – An Introduction

Regex had its roots in neuroscience and mathematics and was only implemented in programming in 1968 by Ken Thompson in QED text editor for text search. Now it's part of many programming languages like Perl, Java, Python, Ruby, and JavaScript.

Let’s look at some examples on how regex works.

I'll be using JavaScript in my examples. Now, to pass beginner level, you need to learn all the characters, classes, quantifiers, modifiers and methods used in regex. Here's a link to Mozilla Developer Network's Regular Expression page where you can view a table containing all those. You can also refer to the cheatsheet at the end of this post with most used characters.

Let’s see a simple example with an explanation. This is a regex.

regexregex

This is what the above regex will look for in a line, a character 'B' followed by at least one of any character between (and including) 'a' to 'z', 'A' to 'Z' and numbers 0 to 9.

Here's a sample of matches in a line highlighted:

Basket , bulb, B12 vitamin, BaSO4 , N BC company

The above regex will stop the search at Basket and return a positive response. That's because the global modifier 'g' has to be specified if you want the regex to look into all the possible matches.

Now, let's see how to use this expression in JavaScript. The test method goes: if found a match return true, else false.

 var input = "your test string", regex = /B[a-zA-Z\d]+/;
 if(!regex.test(input))
 alert('No match is found');
 else
 alert('A match is found');

Let's try another method: match returns the matches found in an array.

 var input = "your test string", 
 regex = /B[a-zA-Z\d]+/g, 
 /*I've added the global modifier 'g' to the regex to get all the matches*/
 ary = input.match(regex); 
 if(ary===null)
 alert('No match is found');
 else
 alert('matches are: ' + ary.toString());

How about string replace? Let's try that with regex now.

 var input = "your test string", 
 regex = /B[a-zA-Z\d]+/g;
 alert(input.replace(regex, "#"));

Below is a codepen for you to tweak. Click the "JavaScript" tab to view the JS code.

See the Pen gpLXYY by Preethi (@rpsthecoder) on CodePen.

Regex Exercises

For exercises, you can google “regex exercises” and try solving them. Here’s what to expect when attempting these exercises, according to difficulty levels.

Basic

To me being able to validate a password is enough for starters. So, validate a password for 8 to 16 character length, alphanumeric with your choice of special characters allowed.

Intermediate

This is where you should practice with more real world data and learn few more regex points like lookahead, lookbehind assertions and matching groups;

  • Validate PIN codes, hexadecimals, dates, email ID, floating point.
  • Replace trailing zero, whitespaces, a set of matching words
  • Extract different parts of a URL
Advanced

You can optimize the above exercises' solutions – the most optimum regex for email has thousands of characters in it – so take it as far as you feel comfortable with and that's enough. You can also try:

  • Parsing HTML or XML (eventhough in the real world it is discouraged to do so because using regular expression to parse non-regular language like HTML will never make it foolproof. Plus XML parsing is a difficult task, more suitable for advanced level users)
  • Replacing tags
  • Removing comments (except the IE conditional comments)

Regex Tools

Tools to visualize regex are one of the coolest things out there for me. If you ever come across a long complex regex, just copy paste them into one of those tools and you'll be able to view the flow clearly. Besides that, there are many tools that you can use to fiddle with the regex code. They also showcase examples and cheatsheets along with share features.

  • Debuggex – It draws a regex diagram as per your input and you can make a quick share to StackOverflow right from there.
  • RegExr – You can test your regex with this one. It also got reference, a cheatsheet and examples to help you out.

Regex Cheatsheet

Token Definition
[abc] Any single character a, b or c
[^abc] Any character other than a, b or c
[a-z] Character between(including) a to z
[^a-z] Character except from a to z
[A-Z] Character between(including) A to Z
. Any single character
\s Any whitespace character
\S Any non-whitespace character
\d Any digit 0 to 9
\D Any non-digit
\w Any word character (letter, number & underscore)
\W Any non-word character
(…) Capture everything enclosed
(a|b) Match either a or b
a? Character a is either absent or present one time
a* Character a is either absent or present more times
a+ Character a is present one or more times
a{3} 3 occurences of character a consecutively
a{3,} 3 or more occurences of character a consecutively
a{3,6} 3 to 6 occurences of character a consecutively
^ Start of string
$ End of string
\b A word boundary. If a character is a word’s last or first word character or If a character is between a word or non-word character
\B Non-word boundary

The post Beginner’s Guide to Regular Expression (Regex) appeared first on Hongkiat.

01 May 05:20

Free Business Card Template: Malam 3 Colors

by ibrandstudio
A double sided free business card template. Malam is come with 3 different color schemes and would be great for a large variety of uses. Enjoy!
11 Apr 15:54

40 Photos That Tell You Why Men Live Shorter Lives Than Women

by Anders Ross

In general perception men are considered stronger than women. Men may lift heavier weight, fight like a champion, run faster, and talk louder, but nowadays women outlive men by at least five to ten years.

40 Photos That Tell You Why Men Live Shorter Lives Than Women

It’s known fact that men’s lives are shorter than women’s and many medical explanation support this statement. But what’s interesting is that apart from these medical explanation there are some other bloody reasons that make men’s live shorter.

Let’s have a look at some of the top reasons why men’s lives are shorter than women.

40 Reasons Why Men Live Shorter Lives Than Women

01.

Why Men Live Shorter Lives Than Women

02.

Why Men Live Shorter Lives Than Women

03.

Why Men Live Shorter Lives Than Women

04.

Why Men Live Shorter Lives Than Women

05.

Why Men Live Shorter Lives Than Women

06.

Why Men Live Shorter Lives Than Women

07.

Why Men Live Shorter Lives Than Women

08.

Why Men Live Shorter Lives Than Women

09.

Why Men Live Shorter Lives Than Women

10.

Why Men Live Shorter Lives Than Women

11.

Why Men Live Shorter Lives Than Women

12.

Why Men Live Shorter Lives Than Women

13.

Why Men Live Shorter Lives Than Women

14.

Why Men Live Shorter Lives Than Women

15.

Why Men Live Shorter Lives Than Women

16.

Why Men Live Shorter Lives Than Women

17.

Why Men Live Shorter Lives Than Women

18.

Why Men Live Shorter Lives Than Women

19.

Why Men Live Shorter Lives Than Women

20.

Why Men Live Shorter Lives Than Women

21.

Why Men Live Shorter Lives Than Women

22.

Why Men Live Shorter Lives Than Women

23.

Why Men Live Shorter Lives Than Women

24.

Why Men Live Shorter Lives Than Women

25.

Why Men Live Shorter Lives Than Women

26.

Why Men Live Shorter Lives Than Women

27.

Why Men Live Shorter Lives Than Women

28.

Why Men Live Shorter Lives Than Women

29.

Why Men Live Shorter Lives Than Women

30.

Why Men Live Shorter Lives Than Women

31.

Why Men Live Shorter Lives Than Women

32.

Why Men Live Shorter Lives Than Women

33.

Why Men Live Shorter Lives Than Women

34.

Why Men Live Shorter Lives Than Women

35.

Why Men Live Shorter Lives Than Women

36.

Why Men Live Shorter Lives Than Women

37.

Why Men Live Shorter Lives Than Women

38.

Why Men Live Shorter Lives Than Women

39.

Why Men Live Shorter Lives Than Women
Why Men Live Shorter Lives Than Women

40.

Why Men Live Shorter Lives Than Women

BONUS

Why Men Live Shorter Lives Than Women

If you care about your life, then SHARE these facts about why men live shorter than women so others can learn how to increase days in their life.

Visit us at InstantShift.com

PSD to HTML

15 Mar 02:15

Deadspin Asshole Spectator Grabs Cyclist's Handlebars, Causes Broken Collarbone | Gizmodo Take The I

by Jane-Claire Quigley
27 Feb 06:36

Fujifilm Instax 210 – shoot and print camera brings instant gratification

by Nigel

fujifilminstax210

So you thought the age of instant photo printing died along with the Polaroid name, did you? Hah! Over there in Japan it still lives, and what’s more it seems to be even flourishing in some bizarre way. Which is very cool. Maybe it’s the idea that you can deliver your keepsake of the moment to your friends and family without having to email or ‘share’ or whatever. Here you go, here’s a print out. [...]


171 words | permalink | No comments | digg this

16 Feb 15:56

Latest Free Resume PSD Templates

by rockerunidev
Here is yet another article that you can rightly categorize as freebie! Today, we planned to update our freebie series with the best resume PSD Templates.The name itself says it all! In today’s design world there is no end to creative ideas, even resumes have got gone immensely artistic and visually cool.
21 Jan 03:01

Photo



09 Sep 16:08

Why Slurping is the Best Way to Eat Ramen Noodles

by Dave Greenbaum

Ramen is extremely popular, both at home and in restaurants, but you might be eating it wrong. The best way to eat ramen is by slurping, much to the chagrin of traditional table manners.

Read more...








29 Jun 10:29

Micromuff

by mark

I use a small camera (Cisco Flip) to take video. It’s great for what I do, except small amounts of wind cause a lot of noise.

That’s where Micromuff helps. You have a small Velcro patch that glues to your camera, and a wind muff that attaches to the Velcro. I’ve been using MicroMuff Original for about six months, and it’s brilliant. I can hear people talking, not wind blowing.

I don’t think there’s anything similar, unless you’re going for professional external microphones and “proper” wind muffs. But even then this is handy because it makes syncing audio easier.

-- Dan Beale-Cocks

Micromuff
£12.95

29 Jun 10:29

Creative Good Founder & CEO Mark Hurst [Cool Tools Show #4]

by mark

CEO and Founder of Creative Good, Mark Hurst brings a laundry list of Cool Tools to our show this week. Our highly productive discussion yields tips on how to properly rinse your text, type most efficiently and how you might casually pick up Mandarin Chinese in your spare time.

Subscribe to the Cool Tools Podcast on iTunes | RSS | Transcript

Show Notes:

Mark’s Website

Mark’s Book, Customers Included

Twitter: @MarkHurst

Here are Mark’s tool picks, with quotes from the show:

Default Folder X: $35

“You know how when you save a file it brings up that little popup dialogue box and it shows the file hierarchy? If you wanna save that file in a particular folder, generally you have to click through this morass of folders… Default Folder makes that process much easier by letting you set hot keys to go to your most commonly accessed folders with one keystroke.”

TextWrangler: Free

“How often do you go to a webpage and you copy some text and you place it in wherever you’re gonna place it, in Word or somewhere else, and suddenly it has the crazy font and italics and the blue color that the original source had an you go, “No, no, no, no! All I want was the text!” And there’s no easy way to remove all that stylistic data. What you do is just paste it in a Text Wrangler file and then copy and paste it out of Text Wrangler to wherever you’re going to put it. I think of it as rinsing off the text until you’re left with the pure ASCII.”

 

Instapaper: $10

“It’s clever enough to save if an article is broken up into three or four click-throughs. It’ll pick up all of those pages’ content and put it in one long scrolling list and it does a text rinse…and displays it without all of the cruft that comes up in a lot of the content sites. One of the best things on my iPhone, bar none is Instapaper.”

Pinboard.in: $10

“With one keystroke. I can tag that URL and get back to it anytime later. All the bookmarks are saved in the cloud. Really simple elegant design. I just love the service. ”

Ask MetaFilter: $5

“Every time I go on somebody’s saying, ‘Does anybody remember that movie? The guy looks to the left and then a watermelon hits the sidewalk.’ And in three minutes someone gives a link to the YouTube clip of that happening. ”

KTdict+ C-E: $4

“If you’re an English speaker learning Chinese, you have three things to memorize, the Chinese character, the definition and then the Pinyin, which is the phonetic pronunciation, including the tone. Most flashcards will give you two sides to the card, but this one actually does three sides…”

 

eStroke: $7

“What it does that the dictionary app does not do is it will show an animation of a character being drawn, and that’s indispensable. ”

Healing Back Pain: The Mind-Body Connection by John E. Sarno: $7

“I was sitting down to breakfast with a friend of mine who said, “Oh, you have back pain? No problem! You just need to read the book!” and [I said] “What book? I don’t wanna read a book!” But he convinced me to read this book and I read it from front to back. The book is very clear: you just need to read the entire book. A few weeks later my back pain disappeared and it really hasn’t come back.”

03 Apr 01:47

iPhone Clip-On Screen Magnifier – pfaff…who needs a large screen launch from Apple?

by Nigel

iphonecliponscreenmagnifier

All eyes are on Cupertino as we wait with bated…er…baited breath for the launch of the next iteration of the iPhone. Rumors are it will be the first time we see a large screen model from Apple, overturning years of the company’s stubborn refusal to bow to market demand. But perhaps it’s already too late?

This iPhone Clip-On Screen Magnifier arguably removes the need to upgrade to a 5 inch screen by delivering super high resolution, [...]


146 words | permalink | One comment | digg this

continue reading
06 Mar 15:52

Wiring Complete

by mark

The clearest, most intelligible, most up-to-date, step-by-step instructions of how to wire most household electrical jobs. Heavily (1,000 photos), smartly illustrated. Besides unraveling the complexities of 3-way switching (I always need help with this), this second-edition deals with other wiring besides electrical power: cable, phone, ethernet. Despite the wireless era, I’ve got more wires in our home every year, and this book has encouraged me to tackle them myself. The guide is supremely practical, full of great tips for working with real wires in real walls. It helped me figure out how to tap a power outlet inside my house for an outdoor line. I can’t think of anything it misses.

-- KK

[This is a Cool Tools Favorite from 2014]

Wiring Complete
Michael Litchfield, Michael McAlister
2013, 272 pages

Available from Amazon

Sample Excerpts:

In some old houses, the neutral wires — rather than the hot wires — may be attached (incorrectly) to receptacles or switches, in violation of code. So when testing existing receptacles, switches, or fixtures, test /all/ wires for voltage.

6

*

5

Each multimedia connector is to the left of the cable It terminates. From left: RG6 F-connector, dual-shielded RG6 coaxial cable; RJ-45 (eight-pin) jack, Cat 6 UTP data cable; RJ-ll (six-pin) jack, Cat 3 phone cable; two RCA audio jacks (sometimes called banana jacks), 14-gauge low-loss audio cable.

*

4
A nut-driver bit speeds up splicing, but be careful not to over twist wires.

*

3

They then use a plumb laser to transfer marks to the ceiling.

*

2

05 Mar 10:55

"We Aren't Something, Until We Are"

by Adam Dachis

"We Aren't Something, Until We Are"

Sarah Kathleen Peck, writing for the 99u, reminds us that when we want to change something about ourselves—be it an aspect of our personality our of line of work—it doesn't just happen overnight. And everything we believe we are now didn't happen that way, either.

Read more...


    






03 Mar 15:40

The Golden Spiral

by Sacha Greif
13 Feb 15:50

w3schools: The Ugly, the Bad, and the Good

by Louis Lazaris
20 Nov 15:19

Like a Rolling Stone Interactive Video

by Robert Williams
28 Oct 13:50

Make Super-Soil for Your Plants from Diapers

by Mihir Patkar

If you are bad at remembering to water your plants, Grant Thompson has a great hack for you: just use diapers! In the above video, Thompson shows how you can use any pack of diapers to make “super-absorbent hydro-soil” for your plants.

Read more...


    






02 Oct 20:05

US government shuts down

by Lauren Morello

Research disrupted as lawmakers spar over funding.

Nature 502 13 doi: 10.1038/502013a

19 Jul 20:35

Hometown celebs Rudd, Riggle and Sudeikis pay visit to Children's Mercy

by Brianne Pfannenstiel
evkuhl

love em

The boys are back in town. The Big Slick Poker Tournament boys, that is. Paul Rudd, Rob Riggle and Jason Sudeikis were on hand at Children's Mercy Hospital Friday morning to promote their annual fundraiser for the hospital's cancer center — and also to play a few games of air hockey with the patients. "Kansas City is our home; it's where we're from. We love Kansas City, and we want to be connected to it," said Riggle, who donned a blue shirt with the letters "KC" inside a big red heart. "Paul…