You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by "Deng, Lea" <Le...@ccc.govt.nz> on 2016/03/16 05:28:53 UTC

search for particular text in a multi-line string

I'd like to have your suggestions on searching for particular text in a multi-line string.

I found the contains() method does not work:
boolean isContain = multi_line_String.contains(expectedText)

Do I have to break down my multi line string into single lines for the search? I think that may slow down the search.

Or is there an object I could use to declare the multi line, instead of using String?

many thanks,
Kind Regards
Lea Deng

Creating connections to make IT easier for everyone

Lead Test Analyst
IT Regulatory Services Team
Christchurch City Council
New Zealand
DDI: +64 (3) 941 88 55
Extn: 8855
lea.deng@ccc.govt.nz<ma...@ccc.govt.nz>

**********************************************************************
This electronic email and any files transmitted with it are intended
solely for the use of the individual or entity to whom they are addressed.

The views expressed in this message are those of the individual sender
and may not necessarily reflect the views of the Christchurch City Council.

If you are not the correct recipient of this email please advise the
sender and delete.

Christchurch City Council
http://www.ccc.govt.nz
**********************************************************************

Re: search for particular text in a multi-line string

Posted by Dinko Srkoč <di...@gmail.com>.
It seems to me that `contains()` works as expected.

def str = '''foo bar
baz quux'''

assert str.contains('bar')
assert str.contains('baz')
assert str.contains('r\nb')
assert str.contains('xxx') == false

Cheers,
Dinko


On 16 March 2016 at 08:55, Paul King <pa...@asert.com.au> wrote:
> Can you give an example of what isn't working as expected? contains()
> should be good enough if you have the appropriate line endings in your
> expected text. Otherwise regex searching might be the way to go.
>
> Cheers, Paul.
>
> On Wed, Mar 16, 2016 at 2:28 PM, Deng, Lea <Le...@ccc.govt.nz> wrote:
>> I'd like to have your suggestions on searching for particular text in a
>> multi-line string.
>>
>>
>>
>> I found the contains() method does not work:
>>
>> boolean isContain = multi_line_String.contains(expectedText)
>>
>>
>>
>> Do I have to break down my multi line string into single lines for the
>> search? I think that may slow down the search.
>>
>>
>>
>> Or is there an object I could use to declare the multi line, instead of
>> using String?
>>
>>
>>
>> many thanks,
>>
>> Kind Regards
>>
>> Lea Deng
>>
>>
>>
>> Creating connections to make IT easier for everyone
>>
>>
>>
>> Lead Test Analyst
>>
>> IT Regulatory Services Team
>>
>> Christchurch City Council
>>
>> New Zealand
>>
>> DDI: +64 (3) 941 88 55
>>
>> Extn: 8855
>>
>> lea.deng@ccc.govt.nz
>>
>>
>>
>> **********************************************************************
>> This electronic email and any files transmitted with it are intended
>> solely for the use of the individual or entity to whom they are addressed.
>>
>> The views expressed in this message are those of the individual sender
>> and may not necessarily reflect the views of the Christchurch City Council.
>>
>> If you are not the correct recipient of this email please advise the
>> sender and delete.
>>
>> Christchurch City Council
>> http://www.ccc.govt.nz
>> **********************************************************************

Re: search for particular text in a multi-line string

Posted by Paul King <pa...@asert.com.au>.
Can you give an example of what isn't working as expected? contains()
should be good enough if you have the appropriate line endings in your
expected text. Otherwise regex searching might be the way to go.

Cheers, Paul.

On Wed, Mar 16, 2016 at 2:28 PM, Deng, Lea <Le...@ccc.govt.nz> wrote:
> I'd like to have your suggestions on searching for particular text in a
> multi-line string.
>
>
>
> I found the contains() method does not work:
>
> boolean isContain = multi_line_String.contains(expectedText)
>
>
>
> Do I have to break down my multi line string into single lines for the
> search? I think that may slow down the search.
>
>
>
> Or is there an object I could use to declare the multi line, instead of
> using String?
>
>
>
> many thanks,
>
> Kind Regards
>
> Lea Deng
>
>
>
> Creating connections to make IT easier for everyone
>
>
>
> Lead Test Analyst
>
> IT Regulatory Services Team
>
> Christchurch City Council
>
> New Zealand
>
> DDI: +64 (3) 941 88 55
>
> Extn: 8855
>
> lea.deng@ccc.govt.nz
>
>
>
> **********************************************************************
> This electronic email and any files transmitted with it are intended
> solely for the use of the individual or entity to whom they are addressed.
>
> The views expressed in this message are those of the individual sender
> and may not necessarily reflect the views of the Christchurch City Council.
>
> If you are not the correct recipient of this email please advise the
> sender and delete.
>
> Christchurch City Council
> http://www.ccc.govt.nz
> **********************************************************************

RE: search for particular text in a multi-line string

Posted by "Winnebeck, Jason" <Ja...@windstream.com>.
Contains works, but depending on your situation you might need to normalize data. If you don't know where your line breaks are, for example if you want to find "the cow jumped over the moon" but the string is "I read a book. It is called the Cow\nJumped Over the Moon", in this case you need to normalize the input. You can normalize any sequence of whitespace (including newlines) to a single space and do a case insensitive comparison (or convert everything to lower case).

Jason

From: Deng, Lea [mailto:Lea.Deng@ccc.govt.nz]
Sent: Wednesday, March 16, 2016 12:29 AM
To: 'users@groovy.apache.org' <us...@groovy.apache.org>
Subject: search for particular text in a multi-line string

I'd like to have your suggestions on searching for particular text in a multi-line string.

I found the contains() method does not work:
boolean isContain = multi_line_String.contains(expectedText)

Do I have to break down my multi line string into single lines for the search? I think that may slow down the search.

Or is there an object I could use to declare the multi line, instead of using String?

many thanks,
Kind Regards
Lea Deng

Creating connections to make IT easier for everyone

Lead Test Analyst
IT Regulatory Services Team
Christchurch City Council
New Zealand
DDI: +64 (3) 941 88 55
Extn: 8855
lea.deng@ccc.govt.nz<ma...@ccc.govt.nz>


**********************************************************************
This electronic email and any files transmitted with it are intended
solely for the use of the individual or entity to whom they are addressed.

The views expressed in this message are those of the individual sender
and may not necessarily reflect the views of the Christchurch City Council.

If you are not the correct recipient of this email please advise the
sender and delete.

Christchurch City Council
http://www.ccc.govt.nz
**********************************************************************

----------------------------------------------------------------------
This email message and any attachments are for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message and any attachments.