Groovy - Regular expression question

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Groovy - Regular expression question

Dave McGee
Hi there,

I have to deal with a String and try get a number identifier out of the String after the word "Element" has occurred. 

Some examples I am dealing with appear as follows:

random unnecessary data Element1 random unnecessary data
more random unnecessary data Element2 random unnecessary data
..
more and more random unnecessary data Element57 random unnecessary data
random unnecessary data random unnecessary data Element105 more and more random unnecessary data

etc

So I'd like to search for "Element" and get "Element105" for example. Is there an elegant way of doing this with regular expressions in Groovy? 

Any help appreciated!

Regards,
Dave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Groovy - Regular expression question

Dinko Srkoc
On 23 February 2012 01:16, Dave McGee <[hidden email]> wrote:

> Hi there,
>
> I have to deal with a String and try get a number identifier out of the
> String after the word "Element" has occurred.
>
> Some examples I am dealing with appear as follows:
>
> random unnecessary data Element1 random unnecessary data
> more random unnecessary data Element2 random unnecessary data
> ..
> more and more random unnecessary data Element57 random unnecessary data
> random unnecessary data random unnecessary data Element105 more and more
> random unnecessary data
>
> etc
>
> So I'd like to search for "Element" and get "Element105" for example. Is
> there an elegant way of doing this with regular expressions in Groovy?

    def str = """
    your multiline String example
    from above
    """
    (str =~ /Element\d+/).each { println it }

should print:

    Element1
    Element2
    Element57
    Element105

If you're after number identifiers, say '2' in 'Element2', this should
do the trick:

    (str =~ /Element(\d+)/).each { println it[1] }


Cheers,
Dinko

>
> Any help appreciated!
>
> Regards,
> Dave

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Groovy - Regular expression question

Dave McGee
Woa, thanks Dinko!

On 22 February 2012 17:17, Dinko Srkoc <[hidden email]> wrote:
On 23 February 2012 01:16, Dave McGee <[hidden email]> wrote:
> Hi there,
>
> I have to deal with a String and try get a number identifier out of the
> String after the word "Element" has occurred.
>
> Some examples I am dealing with appear as follows:
>
> random unnecessary data Element1 random unnecessary data
> more random unnecessary data Element2 random unnecessary data
> ..
> more and more random unnecessary data Element57 random unnecessary data
> random unnecessary data random unnecessary data Element105 more and more
> random unnecessary data
>
> etc
>
> So I'd like to search for "Element" and get "Element105" for example. Is
> there an elegant way of doing this with regular expressions in Groovy?

   def str = """
   your multiline String example
   from above
   """
   (str =~ /Element\d+/).each { println it }

should print:

   Element1
   Element2
   Element57
   Element105

If you're after number identifiers, say '2' in 'Element2', this should
do the trick:

   (str =~ /Element(\d+)/).each { println it[1] }


Cheers,
Dinko

>
> Any help appreciated!
>
> Regards,
> Dave

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





--
Regards,
Dave
Loading...