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

R

Roy

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
 
W

Wildemar Wildenburger

Jacek said:
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
 
R

Roy

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

Joshua Cranmer

Roy said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top