-3

Sorry for asking this dumb question, but I just want to make sure I am doing right.

What is the difference between

^Sentence.*$

and

^Sentence.*

I usually use the first one, but I want to make sure which is the more appropriate.

helloflash
  • 2,424
  • 2
  • 12
  • 19
JohnnyLoo
  • 757
  • 1
  • 9
  • 19

2 Answers2

1

It's depend of the context (i.e. the string).

The $ means by default: end of the string And the quantifiers, like *, are by default greedy.

If the string doesn't contain a newline character, the two patterns are exactly the same. (in the sense they will match exactly the same strings)

But if your string contain a newline character, the .* will stop before it, because the dot, by default, doesn't match the newline character. So the first pattern will always fail, and the second pattern will only match the first line (if it begins with "Sentence" obviously)

Casimir et Hippolyte
  • 83,228
  • 5
  • 85
  • 113
0

From RegExBuddy;;

^ "Assert position at the beginning of a line (at beginning of the string or before a line break character)"
. "Match any single character that is not a line break character"
* "Between zero and unlimited times, as many times as possible, giving back as needed (greedy)"
$ "Assert position at the end of a line (at the end of the string or before a line break character)"

HTH.

http://www.regular-expressions.info/

Leptonator
  • 3,033
  • 1
  • 33
  • 47