General

Understanding RegEx

Oluwole Adebiyi
3 min read
Understanding RegEx
This article was written over 18 months ago and may contain information that is out of date. Some content may still be relevant, but please refer to official documentation for the latest information.

RegEx, an abbreviation of "regular expressions", are a set of characters which are used to create patterns that can be used to search, find, replace, or validate text.

RegEx is language agnostic, although some features are not available to some languages. You can test out RegEx here before putting it in your code.

We will look at some examples:

RegEx: /dog/

Text: "The dog is big dog"

Regular expressions start with a forward slash / followed by the pattern we want to match, and another forward slash / to end it. In this first example, we want to match a simple pattern: "dog". Our RegEx will highlight the first pattern it matches, which is the bold character in there. 

Sometimes, we want our RegEx to highlight not only the first pattern, but all the patterns that can be found in the string. This is where expression flags are introduced. Expression flags are always introduced after the last forward slash. We will use the global expression flag to identify all patterns.

RegEx: /dog/g

Text: "The dog is big dog"

With the global flag, our pattern identifies all the characters in this string.

Here are some different expression flags that can be used.

  • g - The global expression flag, identifies all the pattern in the text
  • i - The case insensitive flag, identifies a pattern regardless of the case (either lower case or upper case)
  • m - The multi line match, identifies a pattern that spans multiple lines

RegEx: /\d/ or /[0-9]/

Text: "1 2343"

There are two ways to match numbers: the first one, \d, is a pattern that matches any single decimal number. The d is case sensitive- the backward slash is used to escape the character d, which can be used to identify only decimal numbers. The second one [0-9] also matches any decimal number, but in a different way, it starts and ends with a square bracket. We will touch more on square brackets soon. However, we want to identify a number from 0–9. So, like this indicates, we can also do [4-9], which means it will identify a number from 4–9, so it will have this instead of "12343".

If we want to identify a concurrent number, we will have to use some commands. 

RegEx: /\d+/ or /[0-9]+/

Text: "12343"

Adding + to our pattern will highlight all the numbers because it means match 1 or more of the preceding pattern. Commands can help a lot when writing RegEx. I will display a command cheat sheet below.

Commands cheat sheat

CommandDescriptionPatternMatches
.Matches any characterc.ccac, cbc, cgc, cdc, cfc
*Matches 0 or more of the preceding tokencat\d*cat, cat24, cat56
+Matches 1 or more of the preceding tokencat\d*cat24, cat56
?Matches 0 or 1 of the preceding tokencat\d*cat, cat5
^Matches beginning of the text^catcatdaddy, cat mummy
$Matches end of the text^oslos, lagos
[...]Matches any pattern in the brackets[0-9xyz]x, y, 1
[^...]Matches any pattern not in the brackets[^0-9xyz]a, b, cd
[a-z]Matches any character within that range[0-9]0, 1, 2, 3
{a}Matches a preceding pattern a number of (a) timescat{2}catcat
{a,}Matches a preceding pattern a number of (a) times or morecat{2,}catcat, catcatcatcat
{a,b}Matches a preceding pattern between a or bcat{1,2}cat, catcat
.Matches any characterco.lcool, coal
OR operatorcat

With the commands above, we can create complex Regex:

Alphanumeric texts

RegEx: /[0-9A-Za-z]+/

Text: "bensoN123"

Twitter Handle

Twitter handles contain numbers, letters, and underscores, but start with @.

RegEx: /^@[0-9A-Za-z_]+/

Text: "@bensoN123"

Simple Email Validation

RegEx: /^[\w]+@[\w]+.[a-zA-Z]{2,4}$/ Text: "wole@mail.co"

RegEx can be complicated at first, but the more you work with it, the easier it gets. You can explore more options, like negative look behind, and positive look ahead. If you have more questions, be free to reach out to me through my Github.

About the author

Oluwole Adebiyi

Oluwole Adebiyi

Keep reading

View all posts →

Export Your Data from Universal Analytics before you lose it

Introduction In 2023, Google announced the retirement of Universal Analytics and told everyone to migrate to Google Analytics 4, commonly known as GA4. Google Analytics has long been a staple for businesses and website owners seeking insights into their audience and performance metrics. However, with the introduction of Google Analytics 4, there comes a pivotal moment for users of the older Universal Analytics (UA) platform. Difference between UA and GA4 What data will you lose? You will lose ALL of the historical data! By July 1, 2024, Google will delete all UA data, and you will no longer have access to it after that. Also, Universal Analytics will no longer track new conversions, affecting ad campaign performance that relies on these conversions for Smart Bidding. Audience lists from Universal Analytics will also disappear, potentially impacting media activation and performance. API requests tied to Universal Analytics properties will fail, preventing data deletion requests and causing Looker Studio to stop displaying Universal Analytics data. Additionally, Attribution Projects (beta) in Google Analytics will be deleted. How to Export Your Data There are multiple ways to export your data from Google Analytics, such as using the Google Analytics add on for Google Sheets or using BigQuery integration to export historical data from your Universal Analytics 360 property. This article will discuss the easiest way to export your data using the Google Analytics add on from Google Workspace Marketplace. From “Extensions” “Google Analytics” “Create new report” The Google Analytics extension will display a Wizard that requires the following steps: Set a name for your report Then, you select the Google Analytics account Then, the property you want to export Also, there are some optional configuration that helps you filter the data you want to export Then click on “Create Report,” and it will export the report for you Here is an example of the template https://docs.google.com/spreadsheets/d/1zXEBEQQk6TPeGb7 Wm2J0uM0q0XnTdJqrfikCTmYs8c/copy Running the Report After setting up the report template with the necessary configurations, users can run the report to extract their UA data into Google Sheets. Depending on the size of the dataset, the report may take some time to generate, especially for larger datasets spanning multiple years. In such cases, splitting the data into smaller chunks may be necessary to ensure efficient processing. Conclusion As Google transitions from Universal Analytics to Google Analytics 4, businesses and website owners must proactively preserve their valuable historical data. With the impending deletion of all Universal Analytics data by July 1, 2024, failing to export your data in time could result in significant data loss, affecting ad campaign performance and analytics capabilities. By following the outlined methods to export data using tools like the Google Analytics add on for Google Sheets, you can ensure that your historical data is safely archived and accessible for future analysis....

Mark Shenouda3 mins
ToolingTips

How Vim Transformed My Workflow for the Better

Discover how diving into Vim transformed my coding workflow...

Jesús Padrón5 mins
VimToolingSoftware EngineeringTips

Effective Communication Strategies Within The Software Development Organization

Have you ever been in a situation where you thought you were communicating effectively, only to realize later that the other person misunderstood what you were saying? Have you ever communicated with someone only to hear that they felt you provided way too much detail, or that you didn’t provide nearly enough detail? Communication in the workplace is how ideas, updates, directions, etc are transferred to others. Each party in a software development organization has differing needs and expectations when it comes to workplace communication. By learning to tailor your communication to meet the needs of each stakeholder, you can become a more effective communicator and achieve greater success within your organization. The requirements of various parties that you interact with in the workplace can vary wildly depending on several factors. Your awareness of these individualized communication preferences and how you can give each party what they want and need will impact your effectiveness in your daily activities, your perception by others, and even your upward mobility within the organization. That's the power of communication, and why it's so important to master effective communication strategies in the workplace! In this article, we'll explore the different types of stakeholders in a software development organization, the communication strategies that work best for each group, and how effective communication can help you advance your career in the industry. We'll start by discussing the difference between “communication” and “effective communication”, before diving into the different types of stakeholders in a software development organization. Then, we'll explore the communication strategies that work best for each group, and provide actionable tips for improving your communication skills. Communication vs. Effective Communication When it comes to communication, it's important to remember that the intended message is only effective if it's received and understood by the recipient, regardless of their background or level of familiarity with the topic. Effective communication is about sharing thoughts, ideas, opinions, knowledge, and data in a way that ensures that the message is received and understood by the recipient. With effective communication, the sender and receiver leave the exchange feeling satisfied. There is a shared understanding of what was intended to be transmitted by the sender. Stakeholder Types In any organization, you have many different types of parties involved in a software project. Let's group the parties involved in software development into three categories for the sake of clarity: Development Team This consists of individual contributors, project managers, scrum masters, QA testers, UX designers, UI designers, architects, etc. Product Team The product team is made up of a diverse group of individuals, including product owners, business analysts, architects, and more. Executive Team CTO, CEO, etc. Each of these parties requires a different type of communication, a different level, and has different needs from your interactions to allow you to provide value from what you are saying and to for them view you as an effective communicator. Let’s talk a bit about what each of these parties needs, and how you can interact with them in the most meaningful way possible. Development Team This is the most detailed version of the interaction. This group needs to be communicated with on the level of individual tickets and the details of those tickets. When interacting with the development team, it's important to focus on the nitty gritty details of each task, ensuring that everything is sorted through meticulously. With this group, we will sort through specific implementation details. An example of interaction with someone from this group might look like this, “I am currently working on ticket 473, and trying to get the checkbox to behave correctly during testing. I have no blockers currently.” Product Team This group will be communicated with at the level of features and larger increments of work such as project milestones. This group is interested in chunks of a project, milestones, progress on the overall initiative, etc. An example of interaction with someone from this group might look like this, “The team is wrapping up development of the new Project X User Interface and will be moving to the implementation of the functionality next”. Executive Team This group is interested in the conversation at the highest levels of abstraction. Generally, they will be more concerned with things at the overall project level. When updating the executive team, it's important to provide high level updates that summarize progress and focus on next steps. For example, you might say, 'We're making great progress on Showcase X and are on track to complete it soon. Next, we'll be shifting our attention to project Y.' Types of Communication What are some of the types of communication? It’s a great question. When you begin to study various communication styles, you will read about different personality types, and how those personalities interact with the world around them. You might hear things like aggressive, passive aggressive, passive, and assertive communication styles. While understanding these can help you communicate effectively, we will focus on how different roles in a company require different levels of detail and specificity in their interactions. Your Natural Communication Style We all have a natural way that we prefer to communicate. Some are very direct and assertive. We might tend to be very to the point, with no filler, no fluff. Others might naturally tend to be more verbose, to fill in lots of details and context and information. Some naturally meet somewhere in the middle on the spectrum of detail vs direct higher level type of communication. There is no right or wrong answer, but you must be aware of your natural tendencies in conversation, and know how to use those effectively, or tailor your communication style to a specific situation or audience. Benefits of Tailored Communication What are the benefits of tailored communication? The primary benefits of tailoring your communication to different stakeholders are that you can provide each person with what they want and need in a way that resonates with them. For instance, I once had to adapt my communication style when working with a highly detail oriented developer who preferred a more granular level of communication. This eases the amount of effort required by the other party to understand you, and allows them to be more effective in taking your message forward. It increases the perception of your effectiveness, and credibility in their eyes as well. If people know that you are someone who can communicate with multiple parties with varying interests and needs, and do so effectively, you will be trusted with more responsibility, and be given more opportunities. Using Effective Communication To Advance Your Career As you can see, developing effective communication skills is a powerful way to advance your career in the software development industry. How have you seen effective communication impact your work? People who are seen as effective communicators have staying power in an organization. They are viewed as competent and necessary. They are given positions of authority and trusted to get things done. I remember that, when I was just starting out in software development, I struggled to communicate effectively with stakeholders at different levels of the organization. But over time, I learned the value of tailoring my communication to each person's unique needs, and it has paid off in my career in countless ways. Basic Strategies For Improving Your Communication Know your audience When preparing for a presentation or conversation, it's essential to consider your audience and tailor your communication style to their needs. What are some strategies you use to ensure your message is received and understood? Write notes in advance, when possible Draw an outline or even the bulk of what you need to deliver before the time comes. Even if you don’t ultimately use these notes directly, preparing them will help you to distill your thoughts and clarify your message, as well as review that they have the appropriate amount of detail for the intended audience. Practice your delivery Though you will not always be giving a speech, talking through what you plan to say will help you to see gaps, smooth the flow, and make sure that you are comfortable with the material you will be presenting or communicating. Conclusion In this article, we learned about the importance of effective communication, strategies for improving your communication, and the direct and indirect positive impacts these improvements can have on your effectiveness and value in the organization. We explored various strategies and approaches to improve communication. Development in this area can yield amazing results for you as you make the investment to improve your skills. We hope you enjoyed this article, and found it helpful. If you have any questions please feel free to join the discussions going on at starter.dev or on our Discord....

Ken Slachta6 mins
GeneralSoftware Engineer

Web Scraping with Deno

TypeScript is a great choice for writing a web scraper, but using it and web APIs in Node is kind of a pain. But there's an alternative: web scraping with Deno!...

Tom VanAntwerp6 mins
DenoVSCodeTipsTypeScript