Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > regular expression to parse {"hello", "hello world","1hello-2*hello"}

Reply
Thread Tools

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

 
 
Roy
Guest
Posts: n/a
 
      01-06-2008
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
 
Reply With Quote
 
 
 
 
Jacek Wojciechowski
Guest
Posts: n/a
 
      01-06-2008
Roy wrote:

> 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 (\\).

--
Pozdrawiam,
J.W.
 
Reply With Quote
 
 
 
 
Wildemar Wildenburger
Guest
Posts: n/a
 
      01-06-2008
Jacek Wojciechowski wrote:
> 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
 
Reply With Quote
 
Roy
Guest
Posts: n/a
 
      01-06-2008
On Jan 6, 7:59 am, Wildemar Wildenburger
<lasses_w...@klapptsowieso.net> wrote:
> Jacek Wojciechowski wrote:
> > 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


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.


 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      01-06-2008
Roy wrote:
> 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


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.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-07-2008
On Sun, 6 Jan 2008 03:46:51 -0800 (PST), Roy <>
wrote, quoted or indirectly quoted someone who said :

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

--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-07-2008
On Sun, 06 Jan 2008 19:01:25 GMT, Joshua Cranmer
<> 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
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[OT] Using LISP/PROLOG to parse regular expression Man-wai Chang C++ 2 03-03-2012 02:36 PM
Need help with regular expression to parse URLs Neil Java 32 08-13-2009 04:36 PM
Help to find a regular expression to parse po file gialloporpora Python 4 07-06-2009 05:42 PM
Need to parse SQL statements...use regular expression? Justin F Perl Misc 4 03-05-2004 04:43 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57