-2

As the title suggests, what is the meaning of ^\d*(\:d+)?$ and what difference of . in the end means i.e. ^\d*(\:d+)?$.

revo
  • 43,830
  • 14
  • 67
  • 109
  • 1
    You seem to have written the same expression twice, and a different expression (twice) in the title. – Konrad Rudolph Apr 25 '19 at 13:13
  • Is the "." at the end of the second regex part of the regex, or is it the end of the sentence? (Given that you ask specifically about '.' I assume it's part of the regex, but want to be certain) – Daniel Beck Apr 25 '19 at 13:13
  • You can evaluate your expression on , it gives you explanation. You can also add some test cases. – Mincong Huang Apr 25 '19 at 13:15
  • @KonradRudolph It seems that it trims a dot at the end of title. So I put it at the end of first regex. – revo Apr 25 '19 at 13:37

3 Answers3

1

The . at the end of ^\d*(:d+)?$. is most likely the end of a sentence and most likely not part of the regex itself.

Why?: $ is a symbol for the end of a line, so unless you are using regex to match multi-line strings (rare but possible) the . wouldn't match anything because nothing comes after the end of the string.

What does the regex mean?:

  • ^ start of string
  • \d* 0 or more digits
  • (:d+)? possibility of a : followed by one or more d (prob a typo and they meant \d again)
  • $ end of string

this most likly matches patterns like:

  • :1 where \d* can have 0 or more entries
  • 12 where : and stuff after : is optional
  • 1234567890 where \d* can have multiple entries and : and numbers after : are optional
  • 1:2 where you have atleast 1 of everything
  • 1234567890:123 where you have many digits on either side of :
CaffeineAddiction
  • 724
  • 12
  • 27
  • In PCRE when `m` is off `$` has the same behavior as `\Z` which matches the position at the end of input string or before the linebreak after the end of input string. In this case, with `s` flag set, `$.` will match (See [live demo here](https://regex101.com/r/LGIHYm/1)). But I agree that probably it is not part of the regex. – revo Apr 25 '19 at 14:00
  • `unless you are using regex to match multi-line strings (rare but possible)` ... you can also force python to match multi-line strings ... people sometimes use this when trying to commune with ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ – CaffeineAddiction Apr 25 '19 at 14:06
  • `match multi-lines string` has different interpretations i.e. 1) to match multi-line strings so that `$` matches at the end of each line or 2) to match multi-line strings so that `.` matches linebreaks. The former could be achieved by setting `m` (MULTILINE) flag (except in Ruby) and the latter with `s` (DOTALL) flag. – revo Apr 25 '19 at 14:12
0
. Dot Matches any character except linebreaks

Form detail information put your regex here: https://regexr.com/

jatin.7744
  • 345
  • 2
  • 9
0

For such cases as your question, a wonderful solution is to find a regex tester page, e.g. https://regex101.com/

Then:

  • Copy your regex in the respective field (REGEX EXPRESSION).
  • Hover the mouse over each part of the regex.
  • You will see a description concerning the part under the cursor.

Take also a look at the right upper corner, with EXPLANATION heading. It contains very detailed explanation of the regex.

And as far as your regex is concerned: (\:d+) looks a bit strange, because:

  • The colon (:) does not require any \ in front of them.
  • Maybe the proper redaction should be (:\d+) i.e. a colon followed by a sequence of digits?

The regex modified as above would match e.g. source string like 789:456.

For an example see https://regex101.com/r/kKz9oo/1

Valdi_Bo
  • 24,530
  • 2
  • 17
  • 30