2 Replies - 132 Views - Last Post: 06 December 2017 - 11:36 AM Rate Topic: -----

#1 Mulligan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-October 17

Regex quantifier matches

Posted 06 December 2017 - 09:24 AM

Having simple pattern like so: .? and test string just A Why does it match not only an A letter but also an empty string ('')?

This post has been edited by Dormilich: 06 December 2017 - 09:28 AM
Reason for edit:: better use inline code tags here

Is This A Good Question/Topic? 0
  • +

Replies To: Regex quantifier matches

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4140
  • View blog
  • Posts: 13,094
  • Joined: 08-June 10

Re: Regex quantifier matches

Posted 06 December 2017 - 09:28 AM

Because of the ? quantifier.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6448
  • View blog
  • Posts: 26,085
  • Joined: 12-December 12

Re: Regex quantifier matches

Posted 06 December 2017 - 11:36 AM

Please don't create duplicate posts. If you feel that your opening post needs editing or moving use the Report button to request this, or add an additional reply with some clarification.



.? is essentially a contradiction that should be avoided. The dot means any character (expect a line terminator) and the question mark means 'zero or one'. If you test with 'ABC' (and the global modifier, which means keep going) it will match 'A', 'B', 'C' and an empty character, which is pretty pointless.

The question mark is of use with a more detailed regex. For example, Hi( Bob)?! will be a match for both 'Hi Bob!' and 'Hi!' That is, whether ' Bob' is present or not.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1