Regex question: Can someone explain the non-greedy concept?

If I have this TB- 6 AM - 10 AM, how do I get a RegEx only to match "TB- ", i.e, either the first "-\s " or first “\s” and no more?

Dinner to cook! TB-(.*\s?) will, I believe, match

  1. The literal letters TB-
  2. Any letters, followed by the first space the matcher encounters.

Ok, you want to match: [start of string]+T+B+[hyphen/minus]+[space]? Thus from TB- 6 AM - 10 AM you want to retrieve 6 AM - 10 AM?

The simplest method, as all character are literally defined at the beginning of the string and occur only once:

$MyString = "TB- 6 AM - 10 AM".substr(0,4);

Via regex

$MyString = "TB- 6 AM - 10 AM".replace("^.{4}(.*)",$1);

FWIW, in aggregate (i.e. being used in many actions in an action-heavy document, i’d go for the first non-regex approach".

Meanwhile, greedy means matching as much as possible:

$MyString = "The cat sat in the hat.".replace("c.+t",""); // → "The ."

The non-greedy version of the same matches as little as possible:

$MyString = "The cat sat in the hat.".replace("c.+?t",""); // → "The  sat in the hat."

Does that help?

I should say thanks for the prod to do this and use the non greedy versions (*? and +?) of * and + regex match-scoping arguments. These are the same:

$MyString = "The cat sat in the hat.".replace("c.+?t",""); // → "The  sat in the hat."
$MyString = "The cat sat in the hat.".replace("c[^t]+t",""); // → "The  sat in the hat."

The second simulates non-greet by asking "match ‘c’ then any character 1 or more times this _is not ‘t’, then a ‘t’.

I should point out that if line breaks are involved differences might occur. The original concept of a regex acts on a single line, e.g. a paragraph in $Text. Note: back when regex were invented there probably was no screen, so a string is conceptually a line of text, i.e. no line breaks. Regex, grep, etc. have evolved methods to deal with line breaks, i.e. lines within lines so to speak.

So, if trying to work in a context where you want multi-line (or across line boundary) matching you may need to burrow into regex detail a bit more.

Bottom line, it’s generally best to assume that the regex thinks it is matching within a line—i.e. to the next line break if there is one ,or else the end of the string.