Regular expressions, why so difficult?

et myString = "Eleanor Roosevelt";
let myRegex = /(Franklin|Eleanor) (([A-Z]\.?|[A-Z][a-z]+) )?Roosevelt/;
let result = myRegex.test(myString);
или
let myRegex = /(Eleanor|Franklin) .*Roosevelt/;

What can you understand here?
and how it works

Many people fine regular expressions difficult. They are quite a powerful tool and can be both very simple and extremely complex depending on what you’re trying to do.

Using a tool like Regex101 will help considerably when working with regular expressions.

A summary of the various parts of the regex are:

(Eleanor|Franklin) matches either of those two words.
[A-Z]\.? matches a single capital letter A through Z, optionally followed by a period.
[A-Z][a-z]+ matches a single capital letter A through z, followed by one or more lower case letters a through z.
(...)? the ? means the previous item (the expression in the parenthesis here) is optional
.* matches any character any number of times (including 0 times).

If you put them into Regex101 it will give you a more detailed description.

5 Likes

Regular expressions are about half a century old. Wikipedia suggests they originated from formats for mathematical formula. The highly cryptic syntax of regular expressions are more easily understood by computers and half a century ago computers were tremendously less powerful.

1 Like

Understanding Regular Expressions can indeed be challenging for many individuals, but breaking down the given regular expressions can make it more accessible. Let’s dissect the provided regular expressions and explain their functionality:

  1. First Regular Expression:
let myRegex = /(Franklin|Eleanor) (([A-Z]\.?|[A-Z][a-z]+) )?Roosevelt/;

  1. This regular expression is designed to match names in the format “Franklin Roosevelt” or “Eleanor Roosevelt.” Here’s a breakdown:
  • (Franklin|Eleanor): This part matches either “Franklin” or “Eleanor.”
  • (([A-Z]\.?|[A-Z][a-z]+) )?: This part is a bit more complex:
    • ([A-Z]\.?|[A-Z][a-z]+): This section matches an initial uppercase letter followed by an optional period (.) or an uppercase letter followed by lowercase letters. This corresponds to first names like “F.,” “Frank,” or “Eleanor.”
    • ( )?: The entire preceding pattern is wrapped in parentheses with a question mark, making it optional. This allows for matching both full names (with a first name) or just the last name “Roosevelt.”
  • Roosevelt: This part simply matches the last name “Roosevelt.”
  1. Second Regular Expression:
let myRegex = /(Eleanor|Franklin) .*Roosevelt/;

  1. This second regular expression is a simplified version of the first one. It matches names in the format “Eleanor Roosevelt” or “Franklin Roosevelt”:
  • (Eleanor|Franklin): Similar to the first regex, this part matches either “Eleanor” or “Franklin.”
  • .*: This part matches any characters (except for a newline) zero or more times. It represents the space between the first and last names.
  • Roosevelt: Like before, this part matches the last name “Roosevelt.”

How it works:

  • Both regular expressions are designed to be used with the test method on a string (myString). This method returns true if the string matches the pattern and false otherwise.
  • The first regular expression is more flexible, allowing for variations in the first name and capturing it as an optional group.
  • The second regular expression is more straightforward and assumes a direct match between the specified first names and the last name “Roosevelt” with any characters in between.

These regular expressions are used to check if a given string (presumably a person’s name) matches the specified patterns, providing flexibility for different formats while capturing the essential components (first name and last name).

2 years ago I had to figure out regular expressions and write a check for a passport number, somehow I was able to do it)) now I don’t remember what I did there, because I think that dealing with ill-thought-out languages is a waste of time, and reading your answer, I don’t even understand 5% of what is there and how it should work)

Can I ask what your intention was when you made this post?

You asked how the code works, and a couple of members have explained that. But now you say

which leaves me wondering why you bothered asking and what you were hoping to achieve here. Perhaps something has been lost in translation, because I understand English is not your first language, but your response to the members who took the trouble to answer comes across as rude and dismissive.

4 Likes

To achieve a complete understanding, like any student, this is why I am always interested in detailed explanations, and not the usual base, which is impossible to understand, because no one can clearly explain writing code, it’s generally a dead end. For every Java script teacher, English is not their native language. It is also impossible to learn his language, you can only know the words and nothing more))

You are likely to get a detailed answer if you ask a detailed question.

If it’s impossible to understand why keep banging your head against the wall? Find something you can understand.

Probably the definitive guide to regular expressions.

A cookbook. This contains ready made regular expressions for common matches, phone numbers, emails etc.

A very useful site to learn about regular expressions.
https://www.regular-expressions.info/quickstart.html

@kicken has already given you the regex101 link, where you can experiment with regular expressions.

Like anything it takes time and practice. You should be able to atleast get a basic grasp of how to work with them.

Personally I quite enjoy the puzzle aspect to working with regexes.

3 Likes

5 posts were split to a new topic: Programming, why so difficult?

I generally find there are three levels of Regex.

Level 1: You really should have done this without Regular Expressions.
Level 2: You needed regular expressions, but the expression can be pretty legibly read. Probably doable with some basic understanding and something like regex101. Most people stop here.
Level 3: You have wandered off into the deep end of “cover all the possibilities”. Time for a life raft and a book.

1 Like

Or having a “Is this REALLY the best way to handle this, or can I do it better differently” review.

1 Like