What is Regular Expression (2)

Rachel
2 min readFeb 26, 2017

--

/d

After yesterday’s article, you may start to get excited about Regex, but maybe a little bit worried if it will be too hard to memorize and use. After all, we have got enough to do and to remember everyday, haven’t we? Don’t worry, Regex is very simple and everyone can pick it up like a sponge.

Simple String Pattern

The easiest and simplest Regex is just the word or phase pattern you want to search. For example, you want to search ‘Medium’ in your website bookmark, you can simple type ‘medium’ and ‘https://medium.com’ can be found.

You can use the pattern for both letters (abc…), digits (123…) and special characters (& _! …)

dot(.)

The above example has no differences with normal search? How about this one — dot(.). ‘.’ can be used for matching anything except newline. What does this mean? Well, if you forget whether it is ‘microeconomics’ or ‘macroeconomics’ you are looking for, you can type m.croeconomics, so you can find both. Dot(.) means any single letter, digit or special character you want to search. But remember, it can only substitute for one character. If you type 1.2.3.4, you may find 1+2–3+4 or may find 1a2b3c4. If you want to match for more than one character, you can type 1..34.

If you want to search a dot(.) instead of everything, you can use \. Here, \ is an escape to show computer that you want to match dot(.) instead of using dot as a pattern character.

\d vs. \D

You may say: man, dot is useful, but can I just match numbers when I search for phone number instead of everything? You bet. you can use \d to help. When you want to search phone number ‘800–800-’ but forget the last four digits, you can type 800-800-\d\d\d\d to find the number you need. It will show something like ‘800–800–1234’. How about the opposite way? You have your final essay needs to be submit, but you have version essay_v1, essay_v2, … essay_v10000 saved in your computer, and you just want to find essay_vFinal, what should you do? Well, you can not use dot since it will list you everything. Or, if you remember, you can search vFinal. What about you were too excited when you finished the final version, and misspell final to something else?\D will help you here. \D is exactly the opposite of \d, which means it will match everything other than digits. essay_v\D can help you find ‘essay_vp’, ‘essay_vF’ or ‘essay_v@’.

\D and \d can only match one character at a time like dot. You can type \d\d\d\d to match multiple, or I will show you, in the following articles’ about how to match multiple characters at a time.

--

--

Rachel
Rachel

Written by Rachel

Learn to fail or fail to learn

No responses yet