• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Don't understand a regex exercise

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody. Please, help me understand a regex excercise.

Here is an excerpt:



How comes that the output is 0123456 (7 start positions) if we have just 6 chars in the string? Thanks in advance.
 
Saloon Keeper
Posts: 13396
296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because there are seven spots where you can start a sequence of zero or more characters, namely one before every character of the input string, and one after the last character of the string.
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Because there are seven spots where you can start a sequence of zero or more characters, namely one before every character of the input string, and one after the last character of the string.



Are you sure about "one before every character of the input string"? I would formulate "one spot FOR every character of the input string". Actually, i don't understand why we have to consider the one after the last character of the string.
Do you know where it is described or explained in any documentation?
 
Stephan van Hulst
Saloon Keeper
Posts: 13396
296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, but that's the easiest way to think about it, especially when you're working with patterns that can match the empty string.

Let me give you a trivial example. Say we want to find all the instances of any character repeated *exactly* zero times. This would be the pattern: ".{0}". Let's say we want to find all the substrings of "0123" that match this pattern. Think about it, where in this input string can we find any character repeated exactly zero times?

Well, there's an empty string before the 0; there's one between 0 and 1; between 1 and 2, and 2 and 3. Finally we can also find an empty string right after 3. If it makes it easier to visualize, think of the input string like this: "" + "0" + "" + "1" + "" + "2" + "" + "3" + "".
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:No, but that's the easiest way to think about it, especially when you're working with patterns that can match the empty string.

Let me give you a trivial example. Say we want to find all the instances of any character repeated *exactly* zero times. This would be the pattern: ".{0}". Let's say we want to find all the substrings of "0123" that match this pattern. Think about it, where in this input string can we find any character repeated exactly zero times?

Well, there's an empty string before the 0; there's one between 0 and 1; between 1 and 2, and 2 and 3. Finally we can also find an empty string right after 3. If it makes it easier to visualize, think of the input string like this: "" + "0" + "" + "1" + "" + "2" + "" + "3" + "".



Yeap, right you are! Visually it's easier to imagine. As i have understood, it has to be taken as a rule of work with empty character pattern. It would be great to find that in an official documentation. In case, i'll find something, i'll share it. For now, thanks for explanation.
 
Author
Posts: 116
11
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regular expression can be confusing. I use this site (http://www.regular-expressions.info/) to learn about regex and this one (http://www.regexplanet.com/advanced/java/index.html) to test the expressions online.

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic