Need help with hard regex

Asked
Active3 hr before
Viewed126 times

5 Answers

regex
90%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Making statements based on opinion; back them up with references or personal experience.,Connect and share knowledge within a single location that is structured and easy to search., That depends on whether the input already is already filtered, because the asker's example doesn't include these cases. (And updated to further simplify the test.) – kennytm Jan 17 '10 at 14:00

'/^[A-Z][a-z]+( [A-Z][a-z]+)*$/'

According to updated requirements:

'/^[A-Z][a-z]*( [A-Za-z][a-z]*)*$/'
load more v
88%

What if you want to find more than one character?,Put them in sequence. Treat them as a substring.,The plus sign (+) in [0-9]+ comes to our rescue. Plus means more than one occurrence of the character or pattern in front of it. In our case, more than one numerals.,The expression does not match any part of the string abc as there are no duplicate characters in sequence. So it returns false.

Do it right now. Just type this into your browser console.

/a/.test("a"); //true
/a/.test("b"); //false
load more v
72%

Here are some reasons given for the difficulty of regular expressions that I don’t agree with.,Regular expressions are challenging, but not for the reasons commonly given.,Regular expressions in Mathematica,Regular expressions in PowerShell and Perl

I think complaints about cryptic syntax miss the mark. Some people say that Greek is hard to learn because it uses a different alphabet. If that were the only difficulty, you could easily learn Greek in a couple days. No, Greek is difficult for English speakers to learn because it is a very different language than English. The differences go much deeper than the alphabet, and in fact that alphabets are not entirely different.

The basic symbol choices for regular expressions — . to match any character, ? to denote that something is optional, etc. — were arbitrary, but any choice would be. As it is, the chosen symbols are sometimes mnemonic, or at least consistent with notation conventions in other areas.

.

I think complaints about cryptic syntax miss the mark. Some people say that Greek is hard to learn because it uses a different alphabet. If that were the only difficulty, you could easily learn Greek in a couple days. No, Greek is difficult for English speakers to learn because it is a very different language than English. The differences go much deeper than the alphabet, and in fact that alphabets are not entirely different.

The basic symbol choices for regular expressions — . to match any character, ? to denote that something is optional, etc. — were arbitrary, but any choice would be. As it is, the chosen symbols are sometimes mnemonic, or at least consistent with notation conventions in other areas.

?
load more v
65%

Programmers think that Regex is hard. As with every skill, it requires practice to master. To help you with it, I wrote this article to cover the basics of Regex and show a simple application of how you can use it.,There are three main traps with regex:,Regex structure and special characters,* - Asterisk character matches any character before it, at least one, i.e., either zero or one.

var pattern = /abc/
load more v
75%

Which regular expression would you say is the bad one and which is the better one? You probably guessed right, so how about a harder question: How much worse would you say the bad one is from the better? What kinds of input would cause the bad regex to perform much worse than the other?,8. There is nothing else for it to try, so it finally fails to match.,To demonstrate this, let’s walk through what each regex does with the following non-matching input:,These regular expressions are written to match text in the following format:

Consider these two regexes:

.*(.*)\[(.*)\]: .*
load more v

Other "regex-undefined" queries related to "Need help with hard regex"