正则表达式 示例_通过3个示例使正则表达式对初学者而言并不那么令人恐惧

正则表达式 示例

When I went to Flatiron School, I was learning all sorts of brand new things about coding/programming. There were a lot of foreign concepts I had to tackle and figure out both on my own and with the help of others. However, nothing looked more scary and overwhelming to me than regular expressions, commonly called “regex”. Let’s see an example of a regex from a really great resource, RegexBuddy (found here):

当我去Flatiron学校时,我正在学习各种有关编码/编程的崭新事物。 我不得不自己解决许多国外概念,并在他人的帮助下弄清楚。 但是,没有什么比正则表达式(通常称为“ regex”)更令人恐惧和不知所措了。 让我们来看一个来自非常有用的资源RegexBuddy的正则表达式的示例(在此处找到):

/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}\b/gi

According to RegexBuddy, this regex is code that searches for and only matches valid email addresses. According to every developer just starting out, this regex looks like a foreign language only to be understood by machines and super genius coders. Symbols and slashes of both variety all over the place, and the only pattern someone might recognize is A-Z or 0–9, outside of that, no words at all.

根据RegexBuddy,此正则表达式是用于搜索且仅与有效电子邮件地址匹配的代码。 根据刚开始的每个开发人员的说法,此正则表达式看起来像是外语,只有机器和超级天才编码人员才能理解。 到处都有符号和斜线,并且有人可以识别的唯一模式是AZ或0–9,除此之外,根本没有单词。

Seems overwhelming, but after looking past the complexities of a long winded regex like the one above, and focusing on the basics, we can find a lot of applications for regular expressions, even for beginner developers who are scared or deterred like I was.

似乎有些让人不知所措,但是在回顾了上述复杂的正则表达式的复杂性并着重于基础知识之后,我们可以找到很多用于正则表达式的应用程序,即使对于像我一样感到恐惧或沮丧的新手开发人员也是如此。

In this post, with JavaScript, I will walk through and break down 3 examples of simple regexes and demonstrate why regular expressions are only as complicated as you make them, and how they might be more useful to new software developers than they might think. This post will not demonstrate every single functionality of regular expressions, but by the end you should feel more comfortable with them and understand more about regex practicality.

在本文中,我将使用JavaScript分解并分解3个简单的正则表达式示例,并说明为什么正则表达式只像您使它们一样复杂,以及它们对新软件开发人员的作用可能比他们想象的要大。 这篇文章不会演示正则表达式的每一个功能,但是到最后,您应该对它们感到更自在,并更多地了解正则表达式的实用性。

Just to kick things off, regular expressions always appear between two forward slashes, but can have flags (single character letters we add) at the end, outside the second forward slash. Ex.:

只是为了开始,正则表达式总是出现在两个正斜杠之间,但是在第二个正斜杠之外的末尾可以带有标志(我们添加的单个字符字母)。 例如:

const regex = /Hello World/i

In general, what we put between the slashes is like a search query, in this case, I’m searching for the phrase “Hello World”, and the ‘ i ’ is a flag which means that we don’t care whether the letters are uppercase or lowercase (the ‘ i ’ is for either ignore case, or case-insensitive). In other words, the string “hellO WOrlD”, would pass our regex test.

通常,我们在斜线之间输入的内容就像一个搜索查询,在这种情况下,我正在搜索短语“ Hello World”,而“ i”是一个标志,这意味着我们不在乎字母是否是大写或小写(“ i”表示忽略大小写或不区分大小写)。 换句话说,字符串“ hellO WOrlD”将通过我们的正则表达式测试。

Now lets get into some examples!

现在让我们来看一些例子!

例子1 (Example 1)

Let’s say you are given a large string of characters, like genetic code, which is all just A’s, T’s, C’s and G’s. Maybe it looks something like this:

假设您有一大串字符,例如遗传密码,它们全都是A,T,C和G。 也许看起来像这样:

const geneticCode = "ATCGGCACTAGCATTATAGCTATATCGGCGCGCGATATCGATCGCGTATCAGTCGTAGATCGATCGACGATCGTATGCTGTCAGCATTAGCTAGCTAGCTGATTGTGTGTACTAGCTAGCTACGTTATATTCGATCGTGCTAGTACGATCGTAGCTACTACTAGCTAGTCATCGATCGTAGCTGATCGTGCTAGTCGCTAGCATGATCGTACGTGACGATCGTACTCACTATCATCGATACTGACATCATCGATCGATCGATCGTAGCATATCGATCGATCGACGTACGTAGCTGACTGACTATCGACGAACATTAGTGATGACTACGACGATCGAGC"

And then let’s pretend we are looking for a specific pattern of letters, to indicate whether someone is a high risk for a certain disease or not. We’ll say that pattern is “CATTAG” in efforts to lighten up this example.

然后,让我们假装我们正在寻找一种特定的字母样式,以表明某人是否患有某种疾病的高风险。 我们将称这种模式为“ CATTAG”,以简化此示例。

Our job is to find out if “CATTAG” appears in this genetic code, and if it does, also find out how many times. So let’s search through our string with a regular expression and get some useful information back using a couple different methods:

我们的工作是查明“ CATTAG”是否出现在该遗传密码中,如果确实出现,则还要找出多少次。 因此,让我们使用正则表达式搜索字符串,并使用几种不同的方法返回一些有用的信息:

const dnaRegex = /CATTAG/gdnaRegex.test(geneticCode) // trueconst matches = geneticCode.match(dnaRegex) // ["CATTAG", "CATTAG"]const totalMatches = matches.length // 2

So what happened here? We made a regex that matches the “CATTAG” pattern, and added a ‘ g ’ flag at the end which stands for global and means it will return every instance of “CATTAG” found in a string.

那这里怎么了 我们制作了一个与“ CATTAG”模式匹配的正则表达式,并在末尾添加了一个“ g”标志,该标志表示global,这意味着它将返回字符串中找到的每个“ CATTAG”实例。

On the next line, we used the “test” method on our regex, and passed it the genetic code string. The “test” method looks through the string for any matches, and if it finds at least one, it returns ‘true’, and obviously if it finds none it returns ‘false’.

在下一行,我们在正则表达式上使用了“测试”方法,并将遗传密码字符串传递给了它。 “ test”方法在字符串中查找是否有任何匹配项,如果找到至少一个匹配项,则返回“ true”,显然如果没有找到,则返回“ false”。

Similar to the “test” method, the “match” method looks through the string for matches, except it returns all matches in an array, instead of a boolean. In this case, it returned two matches in an array.

与“ test”方法类似,“ match”方法在字符串中查找匹配项,不同之处在于它返回数组中的所有匹配项,而不是布尔值。 在这种情况下,它在数组中返回了两个匹配项。

The last line should look familiar to anyone who has worked with JavaScript. We are checking the length of the array created from the “match” method, effectively counting how many matches are in it.

对于使用JavaScript的任何人,最后一行应该看起来很熟悉。 我们正在检查通过“ match”方法创建的数组的长度,以有效地计算其中包含多少个匹配项。

And now we have the information we were asked to retrieve; this genetic code has two matches for the pattern “CATTAG” which we’ll say makes this candidate a low risk for the disease we’re testing for.

现在,我们有了被要求检索的信息; 该遗传密码与模式“ CATTAG”有两个匹配,我们将说这使该候选基因处于我们正在测试的疾病的低风险中。

例子2 (Example 2)

This time we are given a books worth of text (in this case, we’ll use a paragraph worth of our pretend book for demonstration purposes). The author wants to change the name of a certain character, which means finding and changing every single time his/her name is mentioned throughout the entire book. Our job is to change the name “Matthew” to “Ethan”, except sometimes he is also referred to as “Matt”. Regular expressions are our friend, and we can use one here to easily replace this character’s name, regardless of him having a nickname.

这次,我们得到了一本有价值的文字书籍(在这种情况下,我们将假装书籍的一段文字用于演示)。 作者希望更改某个人物的姓名,这意味着在整本书中每一次提及他/她的名字时都进行查找和更改。 我们的工作是将名称“ Matthew”更改为“ Ethan”,除了有时他也被称为“ Matt”。 正则表达式是我们的朋友,我们可以在此处使用一个表达式轻松替换该角色的名字,而不管他的昵称如何。

Here is our text (Lorem ipsum text with “Matt” and “Matthew” sprinkled in):

这是我们的文本(带有“ Matt”和“ Matthew”的Lorem ipsum文本):

const bookText = "Lorem ipsum dolor sit Matthew, consectetur adipiscing elit. Ut euismod erat ante, at viverra tortor vestibulum vitae. In suscipit arcu magna, vel auctor Matthew varius eu. Ut placerat id libero ac sodales. Matt ipsum nisi, ullamcorper at tempus sed, mollis eu libero. Praesent faucibus tortor risus, a rutrum urna mollis at. Integer accumsan orci vitae condimentum facilisis. Sed eu fringilla Matt. Cras sapien ipsum, sollicitudin non sem sed, convallis feugiat quam. Nam Matthew diam vitae odio euismod elementum. Matthew molestie tempor libero sed consequat. Suspendisse mattis commodo lobortis. Sed dapibus aliquet tincidunt. Morbi ut hendrerit nisl. Phasellus tempus nulla pellentesque erat efficitur, ut rhoncus Matthew vestibulum."

And here is how we are going to use our regex:

这是我们将如何使用正则表达式的方法:

const matthewRegex = /Matthew|Matt/gconst newTextWithEthan = bookText.replace(matthewRegex, "Ethan") 
// "Lorem ipsum dolor sit Ethan, ..."

A few new things happened this time. In the regex, i used a pipe symbol (‘|’), which in regex represents “or”. So in other words, our regex matches every instance in the text of the name “Matthew” or “Matt”.

这次发生了一些新情况。 在正则表达式中,我使用了管道符号('|'),在正则表达式中它代表“或”。 因此,换句话说,我们的正则表达式匹配名称为“ Matthew” “ Matt”的文本中的每个实例。

Then, our second constant uses the “replace” method, which takes 2 arguments, the first is the pattern you wish to find, and the second is the text you want to replace it.

然后,我们的第二个常量使用“替换”方法,该方法带有2个参数,第一个是您要查找的模式,第二个是您要替换它的文本。

And we have successfully changed the character’s name to “Ethan” with the help of regular expressions.

并且我们已经借助正则表达式成功地将角色的名称更改为“ Ethan”。

例子3 (Example 3)

As a slightly more complicated example, let’s say we are hired by a company that wants their employees to create a username for their new internal systems. The company has asked that every employee makes their username fit some guidelines, including, that it must start with 4 to 8 lowercase letters, end with exactly 4 numbers, and use 0 symbols and spaces. Our job is to create some code that ensures each employee’s username fits within the company’s guidelines:

举一个稍微复杂的例子,假设我们被一家公司雇用,该公司希望其员工为其新的内部系统创建用户名。 该公司要求每位员工使用户名符合某些准则,包括必须以4到8个小写字母开头,以4个数字结尾,并使用0个符号和空格。 我们的工作是创建一些代码,以确保每个员工的用户名都符合公司的准则:

const validUsername = "smith1992"
const invalidUsername1 = "ted3333" // not enough letters
const invalidUsername2 = "frankie12345" // not exactly 4 numbers
const invalidUsername3 = "alex-4321" // includes a symbolconst usernameRegex = /^[a-z]{4,8}\d{4}$/usernameRegex.test(validUsername) // true
usernameRegex.test(invalidUsername1) // false
usernameRegex.test(invalidUsername2) // false
usernameRegex.test(invalidUsername3) // false

Okay, this regular expression looks a little more complex, let’s break it down bit by bit to decipher how it works:

好的,这个正则表达式看起来稍微复杂一些,让我们对其进行逐点分解以解释其工作方式:

^ — this is a symbol which indicates the string we are testing should start with what comes after it, in this case [a-z]

^-这是一个符号,表示我们正在测试的字符串应以其后的字符开头,在这种情况下为[az]

[a-z] — the brackets allow us to do ranges like a-z which matches any single letter in the lowercase alphabet

[az]-方括号使我们能够进行类似于az的范围,该范围与小写字母中的任何单个字母匹配

{4,8} — this is a quantifier where we have set the lower limit to 4, and the upper limit to 8, meaning we are looking for between 4 to 8 lowercase letters

{4,8}-这是一个量词,我们将下限设置为4,将上限设置为8,这意味着我们正在寻找4到8个小写字母

\d — this is a metacharacter, and the ‘d’ represents digit, so by using this metacharacter, we are essentially saying [0–9], a single digit number between 0 and 9

\ d-这是一个元字符,“ d”代表数字,因此使用此元字符,我们实质上是说[0–9],即介于0和9之间的一个数字

{4} — once again, we are using a quantifier, to tell us how many single digit numbers to look for. Last time, we wanted to use a range of 4 to 8, this time, we want exactly 4 digits, so we don’t need an upper and lower limit

{4}-我们再次使用量词来告诉我们要查找多少个数字。 上一次,我们希望使用4到8的范围,这次,我们希望恰好是4位数字,因此我们不需要上限和下限

$ — opposite of the ^, the dollar sign symbol indicates that the string we are testing should end with what comes right before it, in this case, 4 numbers.

$ —与^相对的美元符号,表示我们正在测试的字符串应以其前面的字符结尾,在本例中为4个数字。

Now we can confidently use this regex in our code to test any username an employee tries to create and get a quick result telling the employee whether they need to fix their username or whether it fits the parameters set by their employer. With that, we have done the job asked of us with one to two lines of code, a regular expression!

现在,我们可以放心地在代码中使用此正则表达式来测试员工尝试创建的任何用户名,并获得快速结果,告知员工他们是否需要修复其用户名或它是否适合其雇主设置的参数。 这样,我们就用一到两行代码(一个正则表达式)完成了我们要求的工作!

结论 (Conclusion)

Hopefully these examples have successfully demonstrated that there are multiple, simple uses for regular expressions, despite how complex they can become. As you can see, it depends on how complicated you choose to make them, or how many conditions you need a particular pattern to meet before it returns any matches.

希望这些例子成功地证明了正则表达式有多种简单用法,尽管它们可能变得很复杂。 如您所见,这取决于您选择制作它们的复杂程度,或特定模式在返回任何匹配项之前需要满足的条件。

This article doesn’t show every single aspect of regular expressions, but there are a lot of resources out there; here’s a cheat sheet that I found especially useful. Remember, even if you are a beginner, regex can help you! It just takes a little time to learn the syntax, but once you do, from what I understand you can use it in most major coding languages, so it’s a versatile developer tool that can be used by any level of developer. If this article helps even just one new dev realize regular expressions are nothing to shy away from, I’ve met my goal. Happy coding!

本文并没有显示正则表达式的每个方面,但是那里有很多资源。 是我发现特别有用的备忘单。 请记住,即使您是初学者,正则表达式也可以为您提供帮助! 学习语法只需要一点时间,但是一旦您了解了语法,就可以在大多数主要的编码语言中使用它,因此它是一个通用的开发人员工具,可以供任何级别的开发人员使用。 如果这篇文章甚至只帮助一个新开发人员意识到正则表达式是不容小from的,那么我已经实现了我的目标。 编码愉快!

翻译自: https://medium.com/@tyler.j.funk2/making-regular-expressions-not-so-scary-for-beginners-with-3-examples-841aaa6528a5

正则表达式 示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值