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 / January 2008

Tip: Looking for answers? Try searching our database.

regular expression to parse {"hello", "hello world",        "1hello-2*hello"}

Thread view: 
Roy - 06 Jan 2008 11:46 GMT
Hi,

I was trying to use Java's regular expression to parse the following
string:

{"hello", "hello world",         "1hello-2*hello"}

I'd like to extract the words inside the quotation marks as follows:

hello
hello world
1hello-2*hello

I've tried different ways to write the expression but didn't work this
out. Can anyone help?

Thanks a lot.
Roy
Jacek Wojciechowski - 06 Jan 2008 15:00 GMT
> I was trying to use Java's regular expression to parse the following
> string:

> hello
> hello world
> 1hello-2*hello

Why do you asking us to do your homework?

Goto: http://java.sun.com/j2se/1.5.0/docs/api/ , class Pattern
and remember about double backslash (\\).

Signature

 Pozdrawiam,
 J.W.

Wildemar Wildenburger - 06 Jan 2008 15:59 GMT
> Why do you asking us to do your homework?

Hey Roy, here's a free tip: It is very likely to get responses like this
when making requests of the form "Please solve my problem!" without
showing and explaining *what you* have tried already and what your exact
problem with your approach is.

regards,
/W
Roy - 06 Jan 2008 16:58 GMT
On Jan 6, 7:59 am, Wildemar Wildenburger
<lasses_w...@klapptsowieso.net> wrote:
> > Why do you asking us to do your homework?
>
[quoted text clipped - 5 lines]
> regards,
> /W

Hi Jacek,
Thank you for the tip.  Actually, this is not my homework. I just
started learning regular expression last night and came up with this
problem. I've tried many ways to parse this string but haven't
succeeded yet.

The problem that bugs me is the spaces inside the quotation marks and
the spaces outside of them. I don't know how to write an expression to
distinguish them. Of course I believe I can find other ways to parse
the string without using any regular expressions. But I am just
curious whether a simple expression can do the job.

Here are what I've tried:

Enter your regex: [^{},"]+
Enter input string to search: {"hello", "hello world",
"1hello-2*hello"}
I found the text "hello" starting at index 2 and ending at index 7.
I found the text " " starting at index 9 and ending at index 10.
I found the text "hello world" starting at index 11 and ending at
index 22.
I found the text "         " starting at index 24 and ending at index
33.
I found the text "1hello-2*hello" starting at index 34 and ending at
index 48.
I found the text " " starting at index 50 and ending at index 51.

Enter your regex: [^{},"\s+]+
Enter input string to search: {"hello", "hello world",
"1hello-2*hello"}
I found the text "hello" starting at index 2 and ending at index 7.
I found the text "hello" starting at index 11 and ending at index 16.
I found the text "world" starting at index 17 and ending at index 22.
I found the text "1hello-2*hello" starting at index 34 and ending at
index 48.
Joshua Cranmer - 06 Jan 2008 19:01 GMT
> Hi,
>
[quoted text clipped - 14 lines]
> Thanks a lot.
> Roy

The simplest regex:

"\\b\\w+\\b"

The "\\b" matches a word boundary (logical, so it actually doesn't match
a character), and the "\\w" matches a word character.

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Roedy Green - 07 Jan 2008 20:06 GMT
On Sun, 06 Jan 2008 19:01:25 GMT, Joshua Cranmer
<Pidgeot18@verizon.invalid> wrote, quoted or indirectly quoted someone
who said :

>"hello", "hello world",         "1hello-2*hello"

if you had a file that looked like that, you could read it as a csv
file.  See http://mindprod.com/jgloss/csv.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 07 Jan 2008 12:26 GMT
>I've tried different ways to write the expression but didn't work this
>out. Can anyone help?

see http://mindprod.com/jgloss/regex.html

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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



©2009 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.