Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-18-2011, 04:29 AM   #1
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Rep: Reputation: 30
Java regex question

Hello all,

I am trying to parse a load of strings using Java regex, the format is pretty much the same however I just dont understand them well enough to finish it. My problem is this, sometimes I have strings with a value or a number, I would like the value in the quotes to return WITHOUT the quotes:

however if it is a number, the string will look like this:

I have an array of them so the matcher group has to be the same, I have come up with various attempts such as:

and set the matcher group to 1 however this doesn't appear to return anything, or I have managed to return the line but with the quotes :S many many thanks in advance.

Old 09-18-2011, 04:33 AM   #2
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
This appears to be a closer match, however doesnt return numbers:

Old 09-18-2011, 04:43 AM   #3
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Take group 2?

Not sure if you need to double-escape the back reference.

doesnt return numbers
This isn't making the "s optional. Also not sure how (.*?)'s working, especially for single characters within the quotes. This might

Also you can nest groups (being as everything's within group0 anyway).
Take group 2, though what do you want to happen when your input is just =" (equals then a single double-quote)?
That's why I suggest the back reference to ensure pairing.

Or you do something with an OR |
Dunno if making the first group non-capturing will affect the nested one. If it doesn't you should be able to always take group 1 for any match.
You might well need to make the [^"] (that's a not doublequote character class) possessive, or at least the second one, so they don't give up their match to allow the right side .* to succeed and leave you with ="anything_with_no_matched_quote.
Again escaping hasn't been checked, might need \" or even \\".

The other way of doing this is to strip the double quote character from the string, replace "\"" with "".

Last edited by Proud; 09-18-2011 at 05:06 AM.
1 members found this post helpful.
Old 09-18-2011, 04:50 AM   #4
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
Ah, nice one. I've changed it to:


works a charm thank you.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 02:10 AM
[SOLVED] java regex for links: trscookie Programming 9 03-24-2011 11:45 AM
[SOLVED] Java Regex nova49 Programming 4 02-02-2011 06:33 PM
Java read binary file in as string for RegEx mulciber Programming 1 12-18-2005 01:36 PM
Java can't find package, but PATH set correctly (j2se1.4.0, java.util.regex package) Ethan Programming 5 02-06-2004 10:55 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:21 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration