How to specify, what coincidence is necessary to search from the beginning of the line?

for example, the desired hello value. How to make world, hello NOT correspond to the search, and hello world match?

just the regular expression /hello/ matches both strings, and you only need the second

1 answer 1

In regular expressions, there are several ways to specify the beginning of a line:

 ^ 

This unshielded circumflex, not immediately after the unshielded open square bracket [ , finds the beginning of the text (string).

However, in Ruby ^ finds only the beginning of the line (line). To find the beginning of the entire text, you must use

 \A 

This anchor can also be used in other regular expression languages ​​/ libraries: in .NET, Python (re, regex), PHP (PCRE), Java, Swift / Objective-C (ICU).

By the way, in text editors such as Notepad ++, ^ also used to find the beginning of a line (line), and not the entire text.

Do not confuse the anchor ^ with other functions of this symbol. Cm.