Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Extract data using regular expression

Thread view: 
On Ali - 20 Apr 2007 11:43 GMT
Hi,

If I have a string like "the name JAVA on the disk". or "the name C++
on the disk.."

I would like to extract JAVA (or C++ or whichever present) from this
string using regular expressions.

How can i do it?

Thanks,

with regards,

on ali
Andrew Thompson - 20 Apr 2007 12:17 GMT
...
>If I have a string like "the name JAVA on the disk". or "the name C++
>on the disk.."

Technically speaking, 'Java' is not an acronym, so
it should not be JAVA - unless you are SHOUTING
at us, in which case, please don't - we are not deaf.

>I would like to extract JAVA (or C++ or whichever present) from this
>string using regular expressions.
>
>How can i do it?

What have you tried so far, and in what way(s) did it
fail for you?  (Note that this is not a help desk.)

Signature

Andrew Thompson
http://www.athompson.info/andrew/

On Ali - 20 Apr 2007 13:35 GMT
> ..
>
[quoted text clipped - 17 lines]
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1

Of course...

Well I know of different crude ways of doing this.. but i want a
simpler cleaner way...

something like this...

                       Pattern pattern = Pattern.compile("the name
(.*) on the disk");

            Matcher matcher = pattern.matcher(output);

            while (matcher.find()) {
                System.out.println("Found a match: " + matcher.group());
                System.out.println("Start position: " + matcher.start());
                System.out.println("End position: " + matcher.end());
              }

i remember in javascript we can specify multiple match strings... by
either putting square brackets.. etc.
Lew - 20 Apr 2007 13:40 GMT
> i remember in javascript we can specify multiple match strings... by
> either putting square brackets.. etc.

The word "I" is always capitalized in English.

The ellipsis is used to indicate missing, omitted or elided text.

Have you read
<http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html>
?

Signature

Lew

On Ali - 20 Apr 2007 13:59 GMT
> > i remember in javascript we can specify multiple match strings... by
> > either putting square brackets.. etc.
[quoted text clipped - 9 lines]
> --
> Lew

If I do the same thing with Javascript, i will have to do it in the
following manner.

/The name is (,*) on the disk/,exec("The name is Java on the disk")

This would return an array containing the following two strings
- The name is Java on the disk
- Java

I am interested in the second string.

Is there a similar way of doing this with Java
On Ali - 20 Apr 2007 13:59 GMT
> > i remember in javascript we can specify multiple match strings... by
> > either putting square brackets.. etc.
[quoted text clipped - 9 lines]
> --
> Lew

If I do the same thing with Javascript, i will have to do it in the
following manner.

/The name is (,*) on the disk/,exec("The name is Java on the disk")

This would return an array containing the following two strings
- The name is Java on the disk
- Java

I am interested in the second string.

Is there a similar way of doing this with Java
Sunny - 20 Apr 2007 14:19 GMT
Do something like below what you were trying:
Pattern.compile(".*The\\s+name\\s+is\\s+(\\w+)\\s+on\\s+the\\s
+disk.*");

Also see http://java.sun.com/developer/technicalArticles/releases/1.4regex/

Sunny

> > > i remember in javascript we can specify multiple match strings... by
> > > either putting square brackets.. etc.
[quoted text clipped - 22 lines]
>
> Is there a similar way of doing this with Java
Sunny - 20 Apr 2007 14:30 GMT
Use matcher.group(1) to capture Java or C++ words.

Cheers

> Do something like below what you were trying:
> Pattern.compile(".*The\\s+name\\s+is\\s+(\\w+)\\s+on\\s+the\\s
[quoted text clipped - 30 lines]
>
> > Is there a similar way of doing this with Java
Lew - 21 Apr 2007 03:35 GMT
> Use matcher.group(1) to capture Java or C++ words.
>
>> > Do something like below what you were trying:
>> > Pattern.compile(".*The\\s+name\\s+is\\s+(\\w+)\\s+on\\s+the\\s
>> > +disk.*");

A: Because it confuses the reader.
Q: Why is it bad?
A: Placing the response above the quoted material instead of inline.
Q: What is top-posting?

Signature

Lew

Boris Ozegovic - 20 Apr 2007 14:40 GMT
> Well I know of different crude ways of doing this.. but i want a
> simpler cleaner way...
[quoted text clipped - 3 lines]
>                         Pattern pattern = Pattern.compile("the name
> (.*) on the disk");

.* is greedy.  Beware of performance issues, and consider lazy quantifier
for this example, or even better, use class, or just C++|Java...

Signature

Greatest sh.ts:
http://www.net.hr/vijesti/page/2007/03/30/0030006.html

Jeffrey Schwab - 22 Apr 2007 21:57 GMT
>> ..
>>
[quoted text clipped - 35 lines]
> i remember in javascript we can specify multiple match strings... by
> either putting square brackets.. etc.

Quick & Dirty:

    s.replaceAll("the name (\\S+) on the disk", "$1");


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.