Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8169919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:00:16+00:00 2026-06-06T21:00:16+00:00

I’m just starting with Groovy. I’m sure that I’m missing something stupid. Can someone

  • 0

I’m just starting with Groovy. I’m sure that I’m missing something stupid. Can someone tell me why this code fails in the groovy console? It thinks that there are only 14 words and 1 line in the input string.

def input = """
line1: 50 65 42
line2: 123 456 352 753 1825
line3: 10 25 20 48 107
"""

words = input.split(/ /)
lines = input.split(/^/)
assert words.size() == 16
assert lines.size() == 3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:00:18+00:00Added an answer on June 6, 2026 at 9:00 pm

    In the case of the first assert, the error message can hint to where the problem is at:

    Assertion failed: 
    
    assert words.size() == 16
           |     |      |
           |     14     false
           [
           line1:, 50, 65, 42
           line2:, 123, 456, 352, 753, 1825
           line3:, 10, 25, 20, 48, 107
           ]
    

    There are some commas missing there and the way the words value is printed seems wrong. What is happening is that input.split(/ /) is splitting the input string by spaces, but, as it also contains newlines, some words are not split, e.g., '42\nline2:'.

    To see this more clearly, we can use the inspect method, which gives us a string with the literal form of an object:

    println input.inspect()
    // --> '\nline1: 50 65 42\nline2: 123 456 352 753 1825\nline3: 10 25 20 48 107\n'
    
    println input.split(/ /).inspect()
    // --> ['\nline1:', '50', '65', '42\nline2:', '123', '456', '352', '753', '1825\nline3:', '10', '25', '20', '48', '107\n']
    

    To split a word by whitespace Groovy adds a convenience non-parametrized split method:

    def words = input.split()
    assert words.size() == 16
    println words.inspect()
    // --> ['line1:', '50', '65', '42', 'line2:', '123', '456', '352', '753', '1825', 'line3:', '10', '25', '20', '48', '107']
    

    And to get the lines of a string you can use readLines.

    def lines = input.readLines()
    println lines.inspect()
    // --> ['', 'line1: 50 65 42', 'line2: 123 456 352 753 1825', 'line3: 10 25 20 48 107']
    

    Notice, however that readLines will return four elements, as there is a first empty line (i don’t know why it ignores the last empty line though), so the assert lines.size() == 3 will still fail.

    You can filter-out those empty lines using Collection#findAll() on the resulting list, or calling String#findAll(Pattern) directly on the input string:

    def lines = input.readLines().findAll()
    assert lines.size() == 3
    
    def lines2 = input.findAll(/\S+/)
    assert lines2.size() == 3
    
    assert lines == lines2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I know there's a lot of other questions out there that deal with this
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.