LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 09-18-2011, 03:29 AM   #1
trscookie
Member
 
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:

Code:
="myvalue"
however if it is a number, the string will look like this:

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

Code:
=(?")(.*?)(?")
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.

trscookie
 
Old 09-18-2011, 03:33 AM   #2
trscookie
Member
 
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, 03:43 AM   #3
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
=("?)(.*)(\1)
Take group 2?

Not sure if you need to double-escape the back reference.
http://download.oracle.com/javase/6/...x/Pattern.html

Quote:
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 04:06 AM.
 
1 members found this post helpful.
Old 09-18-2011, 03:50 AM   #4
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

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

=("?)(.*)(\\1)

works a charm thank you.
 
  


Reply


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 01:10 AM
[SOLVED] java regex for links: trscookie Programming 9 03-24-2011 10:45 AM
[SOLVED] Java Regex nova49 Programming 4 02-02-2011 05:33 PM
Java read binary file in as string for RegEx mulciber Programming 1 12-18-2005 12:36 PM
Java can't find package, but PATH set correctly (j2se1.4.0, java.util.regex package) Ethan Programming 5 02-06-2004 09:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:29 PM.

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