• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

split finding strange "null"

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From WhizLabs prep exam 4, question 2I know what "a{3}" means and so I thought the string would be split thus: "[aaa][aaa][aaa]bb". Giving 3 Strings in the s array with values of "", "" and "bb".
I was wrong. The answer was 4, but WhizLabs did not really explain why (they just stated it was 4), I now realise that I had forgotten about the leading "null" match before the first "aaa" (even though it should have been obvious to me from my diagram.

I startes looking a bit further at this I am obviously have some serious hole in my regex knowledge (but it's late....)Answer is 0, not 3 or 4. Why?
Answer is still 0, probably for the same reasons as above.Answer is now 2, there seems to be a match before the first "aa", but there is no match after the last "aa". Why is that?Answer is now 3, there's just the match before the first "aa".
Is there some crazy regex rule I should tattoo on to my frontal lobes about when it will and will not match/find nulls at the beginning/end?

I am feeling less confident about this exam with every passing day.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there some crazy regex rule I should tattoo on to my frontal lobes about when it will and will not match/find nulls at the beginning/end?



Good news... it is actually not a regex rule... so your knowledge of regex is intact...

From the JavaDoc for string.split...

Splits this string around matches of the given regular expression.

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.



Looking at the other split that defines a limit (to see what zero does)...

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.



So... the split() method that takes one param defines a limit that deletes "trailing empty strings".

Henry
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent, that explains everything. So "aaa" .split("a") does split, but because the result is effectively an array of trailing empties they don't get returned.

Now seared onto left frontal lobe.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't the split string in the last 2 examples of the openingspost also be 'aa' or 'a{2}', to match the number of found strings given?
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic