Results 1 to 5 of 5
Thread: Regular Expression Problem
- 02-28-2012, 04:23 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Regular Expression Problem
I have an Arraylist of strings and I am trying to use some sort of regex expression to split the strings up into 5 parts.
Sample strings:
KRAFT BREYERS Lowfat Strawberry Yogurt (1% Milkfat) 96 18.2 3.8 0.8
Milk whole, 3.25% milkfat 61, 4.78 3.15, 3.27
Yogurt plain, 13 grams protein per 8 ounce 56 7.68 5.73 0.18
Ground turkey 93% lean 7% fat pan-broiled crumbles 213 0 27.1 11.6
I am trying to split the sting based on the last 4 numbers in every string. So if I were to split the first sting in the list above it would look like:
KRAFT BREYERS Lowfat Strawberry Yogurt (1% Milkfat)
96
18.2
3.8
0.8
I tried something like (?<![a-z])\s\d\s(?<![a-z]), but that doesn't seem to work. Any ideas?
- 02-28-2012, 07:15 PM #2
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Regular Expression Problem
Regex might not be the right tool for the job here. Instead, consider what you know about the string -
Perhaps you could work with this knowledge instead.I am trying to split the sting based on the last 4 numbers in every string.
- 02-28-2012, 08:31 PM #3
Re: Regular Expression Problem
Are the commas in your second input sample intentional? I'm guessing, not, but I've catered to it anyways -- except for eliminating the comma from the final (split) output. Post back if my guess is wrong.
Brief explanation: split on whitespace when followed by (one to four ((any number of digit/dot/comma) + zero or one whitespace)) followed by end of input.Java Code:"\\s(?=([\\d.,]*\\s?){1,4}$)"
db
edit This removes the (zero or one) comma that precedes the whitespace.Java Code:"(?:,?)\\s(?=([\\d.,]*\\s?){1,4}$)"Last edited by DarrylBurke; 02-28-2012 at 08:36 PM.
Why do they call it rush hour when nothing moves? - Robin Williams
- 02-28-2012, 08:54 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Re: Regular Expression Problem
Thanks DarrylBurke, this seems to work. I guess I was a lot farther away from getting this to work than I thought.
The comma is intentional. One of the database fields is a food description and its an alphanumeric String with punctuation. Then when that description field had 4 numbers appended to it that I needed for a calculation it made it difficult to split. I couldn't figure out how to split off the last 4 numbers while ignoring the numbers in the description field (like percentages).
- 02-28-2012, 10:21 PM #5
Re: Regular Expression Problem
You're welcome. To be honest, I didn't even read your regex -- my regex skills don't extend to interpreting someone else's regex (heck, I can't even fathom most of those I've written!). So I can't comment on how far you were from a workable regex.
If you have a large dataset, you might want to observe for any performance issues. I do know there are greedy/reluctant regex tweaks for improving performance, but I don't know how to apply them.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Regular Expression Help
By niketanand in forum Advanced JavaReplies: 1Last Post: 06-24-2011, 04:56 PM -
problem with using regular expression
By orkun in forum Advanced JavaReplies: 5Last Post: 01-03-2011, 07:59 PM -
regular expression
By prof.deedee in forum JDBCReplies: 3Last Post: 02-19-2010, 11:15 AM -
regular expression
By ras_pari in forum Advanced JavaReplies: 27Last Post: 10-07-2009, 12:25 PM -
Regular Expression Problem
By daflores in forum Advanced JavaReplies: 8Last Post: 02-10-2009, 06:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks