Quantcast

How to extract some part of string using GROOVY ?

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

How to extract some part of string using GROOVY ?

Harsha1
Hi, I am brand new to java but I have a requirement where i am suppose to use Groovy.
So is there any good features that can address my requirement?
I have string like
str = "This is a sample text. Trying to explore groovy. In India, Bangalore, Harsha, N., with experts help";

In the above string am only interested in the Name of the person "Harsha" which will always be followed by letter N. and ","
(N stands for Name which will be after every name)

another example is ,
str = "This is a sample text. Trying to explore groovy. In India, Bangalore, Daniel O' Relly, N., with experts help";

Here I need to extract " Daniel O' Relly" which is followed by N. and "," to continue

So is there any features available in groovy that will do the job??

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

Re: How to extract some part of string using GROOVY ?

Robert Fischer
Sounds like you want a regular expression.

~~ Robert.

Harsha1 wrote:

> Hi, I am brand new to java but I have a requirement where i am suppose to use
> Groovy.
> So is there any good features that can address my requirement?
> I have string like
> str = "Background:  Educator who unsuccessfully tried to obtain a teaching
> position sued county board of education in state court to recover $100,000
> in compensatory damages for alleged age discrimination under the federal Age
> Discrimination in Employment Act (ADEA). Board filed a motion to dismiss.
> The Circuit Court, Baltimore County, Jakubowski, J., granted the motion and
> dismissed educator's claim without prejudice. Educator appealed.";
>
> In the above string am only interested in the judge name "Jakubowski" which
> will always followed by letter J.
>
> So is there any features available in groovy that will do the job??
>
>
--
~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html


signature.asc (201 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to extract some part of string using GROOVY ?

Edward Sumerfield-2
In reply to this post by Harsha1
Here is a good source of code snippets. See the String section and search for "=~" for the regex stuff

    http://pleac.sourceforge.net/pleac_groovy/index.html

More details on regex specifically

    http://groovy.codehaus.org/Regular+Expressions

On Thu, Jun 18, 2009 at 1:31 PM, Harsha1 <[hidden email]> wrote:

Hi, I am brand new to java but I have a requirement where i am suppose to use
Groovy.
So is there any good features that can address my requirement?
I have string like
str = "Background:  Educator who unsuccessfully tried to obtain a teaching
position sued county board of education in state court to recover $100,000
in compensatory damages for alleged age discrimination under the federal Age
Discrimination in Employment Act (ADEA). Board filed a motion to dismiss.
The Circuit Court, Baltimore County, Jakubowski, J., granted the motion and
dismissed educator's claim without prejudice. Educator appealed.";

In the above string am only interested in the judge name "Jakubowski" which
will always followed by letter J.

So is there any features available in groovy that will do the job??


--
View this message in context: http://www.nabble.com/How-to-extract-some-part-of-string-using-GROOVY---tp24095339p24095339.html
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
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

Re: How to extract some part of string using GROOVY ?

Martin C. Martin-2

mystring =~ /\bJ[a-zA-Z]+/

will return a list of all words that start with a capital J.  As Edward
said, you can see more examples here:

http://groovy.codehaus.org/Regular+Expressions

Best,
Martin

Edward Sumerfield wrote:

> Here is a good source of code snippets. See the String section and
> search for "=~" for the regex stuff
>
>     http://pleac.sourceforge.net/pleac_groovy/index.html
>
> More details on regex specifically
>
>     http://groovy.codehaus.org/Regular+Expressions
>
> On Thu, Jun 18, 2009 at 1:31 PM, Harsha1 <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>
>     Hi, I am brand new to java but I have a requirement where i am
>     suppose to use
>     Groovy.
>     So is there any good features that can address my requirement?
>     I have string like
>     str = "Background:  Educator who unsuccessfully tried to obtain a
>     teaching
>     position sued county board of education in state court to recover
>     $100,000
>     in compensatory damages for alleged age discrimination under the
>     federal Age
>     Discrimination in Employment Act (ADEA). Board filed a motion to
>     dismiss.
>     The Circuit Court, Baltimore County, Jakubowski, J., granted the
>     motion and
>     dismissed educator's claim without prejudice. Educator appealed.";
>
>     In the above string am only interested in the judge name
>     "Jakubowski" which
>     will always followed by letter J.
>
>     So is there any features available in groovy that will do the job??
>
>
>     --
>     View this message in context:
>     http://www.nabble.com/How-to-extract-some-part-of-string-using-GROOVY---tp24095339p24095339.html
>     Sent from the groovy - user mailing list archive at Nabble.com.
>
>
>     ---------------------------------------------------------------------
>     To unsubscribe from this list, please visit:
>
>        http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
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

Re: How to extract some part of string using GROOVY ?

Ted Naleid
Groovy 1.6.3 adds String.find to the regular expression toolkit and it's built for this kind of thing:


str = """Background:  Educator who unsuccessfully tried to obtain a teaching
position sued county board of education in state court to recover \$100,000
in compensatory damages for alleged age discrimination under the federal Age
Discrimination in Employment Act (ADEA). Board filed a motion to dismiss.
The Circuit Court, Baltimore County, Jakubowski, J., granted the motion and
dismissed educator's claim without prejudice. Educator appealed."""


str.find(/\b\S+ J\./)


On Thu, Jun 18, 2009 at 1:08 PM, Martin C. Martin <[hidden email]> wrote:

mystring =~ /\bJ[a-zA-Z]+/

will return a list of all words that start with a capital J.  As Edward said, you can see more examples here: Best,
Martin

Edward Sumerfield wrote:
Here is a good source of code snippets. See the String section and search for "=~" for the regex stuff

   http://pleac.sourceforge.net/pleac_groovy/index.html

More details on regex specifically

   http://groovy.codehaus.org/Regular+Expressions

On Thu, Jun 18, 2009 at 1:31 PM, Harsha1 <[hidden email] <mailto:[hidden email]>> wrote:


   Hi, I am brand new to java but I have a requirement where i am
   suppose to use
   Groovy.
   So is there any good features that can address my requirement?
   I have string like
   str = "Background:  Educator who unsuccessfully tried to obtain a
   teaching
   position sued county board of education in state court to recover
   $100,000
   in compensatory damages for alleged age discrimination under the
   federal Age
   Discrimination in Employment Act (ADEA). Board filed a motion to
   dismiss.
   The Circuit Court, Baltimore County, Jakubowski, J., granted the
   motion and
   dismissed educator's claim without prejudice. Educator appealed.";

   In the above string am only interested in the judge name
   "Jakubowski" which
   will always followed by letter J.

   So is there any features available in groovy that will do the job??


   --
   View this message in context:
   http://www.nabble.com/How-to-extract-some-part-of-string-using-GROOVY---tp24095339p24095339.html
   Sent from the groovy - user mailing list archive at Nabble.com.


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

      http://xircles.codehaus.org/manage_email




---------------------------------------------------------------------
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

Re: How to extract some part of string using GROOVY ?

Harsha1
In reply to this post by Edward Sumerfield-2

So is the Regular expression feature in groovy provide more feature than the regular expression of Java??

Edward Sumerfield-2 wrote
Here is a good source of code snippets. See the String section and search
for "=~" for the regex stuff

    http://pleac.sourceforge.net/pleac_groovy/index.html

More details on regex specifically

    http://groovy.codehaus.org/Regular+Expressions

On Thu, Jun 18, 2009 at 1:31 PM, Harsha1 <99harsha.h.n99@gmail.com> wrote:

>
> Hi, I am brand new to java but I have a requirement where i am suppose to
> use
> Groovy.
> So is there any good features that can address my requirement?
> I have string like
> str = "Background:  Educator who unsuccessfully tried to obtain a teaching
> position sued county board of education in state court to recover $100,000
> in compensatory damages for alleged age discrimination under the federal
> Age
> Discrimination in Employment Act (ADEA). Board filed a motion to dismiss.
> The Circuit Court, Baltimore County, Jakubowski, J., granted the motion and
> dismissed educator's claim without prejudice. Educator appealed.";
>
> In the above string am only interested in the judge name "Jakubowski" which
> will always followed by letter J.
>
> So is there any features available in groovy that will do the job??
>
>
> --
> View this message in context:
> http://www.nabble.com/How-to-extract-some-part-of-string-using-GROOVY---tp24095339p24095339.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> 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

Re: How to extract some part of string using GROOVY ?

Russel Winder-2
In reply to this post by Harsha1
Is it fair to assume this is an assignment on a programming course?

On Thu, 2009-06-18 at 10:31 -0700, Harsha1 wrote:

> Hi, I am brand new to java but I have a requirement where i am suppose to use
> Groovy.
> So is there any good features that can address my requirement?
> I have string like
> str = "Background:  Educator who unsuccessfully tried to obtain a teaching
> position sued county board of education in state court to recover $100,000
> in compensatory damages for alleged age discrimination under the federal Age
> Discrimination in Employment Act (ADEA). Board filed a motion to dismiss.
> The Circuit Court, Baltimore County, Jakubowski, J., granted the motion and
> dismissed educator's claim without prejudice. Educator appealed.";
>
> In the above string am only interested in the judge name "Jakubowski" which
> will always followed by letter J.
>
> So is there any features available in groovy that will do the job??
>
>
--
Russel.
=============================================================================
Dr Russel Winder      Partner
                                            xmpp: [hidden email]
Concertant LLP        t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,   f: +44 8700 516 084   voip: sip:[hidden email]
London SW11 1EN, UK   m: +44 7770 465 077   skype: russel_winder

signature.asc (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

instance via String

Andreas Jöcker
In reply to this post by Harsha1
Hi,

calling a method which I pass as a String parameter works - eg

def foo() {
  println 'bar'
}

def l = 'foo'
"$l"()

But how do I instanciate a class with the same mechanism - eg

class Foo {
  def bar() {
     println 'foobar'
  }
}

def l = 'Foo'
new "$l"().bar()

breaks with

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, test.groovy: 8: unexpected token:  @ line 8, column 5
 new "$l"().bar()

how can I create instances with that mechanism ?

Cheers
A

--

Andreas Jöcker
GiS - Gesellschaft für integrierte Systemplanung mbH
Junkersstr. 2
69469 Weinheim

E-Mail   [hidden email]
Telefon +49 6201 503-59
Fax     +49 6201 503-66

Gesellschaft für integrierte Systemplanung mbH
Geschäftsführer: Eckhard Haffmann, Alfred Gai, Bernd Heselmann
Sitz der Gesellschaft: Zeppelinstr. 11 - 91052 Erlangen
Amtsgericht Fürth/Bayern - Handelsregister-Nr. HRB 3435


---------------------------------------------------------------------
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

Re: instance via String

Martin C. Martin-2
I think you need to use Java reflection:

Class.forName("my.package.Foo").getConstructor(...).newInstance(...)

It's a shame that you have to use a completely different syntax, but in
some common cases, the parser needs to know whether a given name starts
with a capital letter during parsing, to know whether its a type or a
method, e.g.:

String x  // declares variable
println x // calls println method

"$blah" x // What does this do?

Best,
Martin

Andreas Jöcker wrote:

> Hi,
>
> calling a method which I pass as a String parameter works - eg
>
> def foo() {
>   println 'bar'
> }
>
> def l = 'foo'
> "$l"()
>
> But how do I instanciate a class with the same mechanism - eg
>
> class Foo {
>   def bar() {
>      println 'foobar'
>   }
> }
>
> def l = 'Foo'
> new "$l"().bar()
>
> breaks with
>
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, test.groovy: 8: unexpected token:  @ line 8, column 5
>  new "$l"().bar()
>
> how can I create instances with that mechanism ?
>
> Cheers
> A
>

---------------------------------------------------------------------
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

Re: How to extract some part of string using GROOVY ?

Martin C. Martin-2
In reply to this post by Harsha1


Harsha1 wrote:
>
> So is the Regular expression feature in groovy provide more feature than the
> regular expression of Java??

Well, it uses the Java regular expression underneath.  So in terms of
what strings it can match, and what information you can get, its the
same.  It just provides a more convenient syntax for it.

Best,
Martin

>
>
> Edward Sumerfield-2 wrote:
>> Here is a good source of code snippets. See the String section and search
>> for "=~" for the regex stuff
>>
>>     http://pleac.sourceforge.net/pleac_groovy/index.html
>>
>> More details on regex specifically
>>
>>     http://groovy.codehaus.org/Regular+Expressions
>>
>> On Thu, Jun 18, 2009 at 1:31 PM, Harsha1 <[hidden email]> wrote:
>>
>>> Hi, I am brand new to java but I have a requirement where i am suppose to
>>> use
>>> Groovy.
>>> So is there any good features that can address my requirement?
>>> I have string like
>>> str = "Background:  Educator who unsuccessfully tried to obtain a
>>> teaching
>>> position sued county board of education in state court to recover
>>> $100,000
>>> in compensatory damages for alleged age discrimination under the federal
>>> Age
>>> Discrimination in Employment Act (ADEA). Board filed a motion to dismiss.
>>> The Circuit Court, Baltimore County, Jakubowski, J., granted the motion
>>> and
>>> dismissed educator's claim without prejudice. Educator appealed.";
>>>
>>> In the above string am only interested in the judge name "Jakubowski"
>>> which
>>> will always followed by letter J.
>>>
>>> So is there any features available in groovy that will do the job??
>>>
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/How-to-extract-some-part-of-string-using-GROOVY---tp24095339p24095339.html
>>> Sent from the groovy - user mailing list archive at Nabble.com.
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>    http://xircles.codehaus.org/manage_email
>>>
>>>
>>>
>>
>

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

    http://xircles.codehaus.org/manage_email


1234
Loading...