Regular Expressions
I'm trying to find a regular expression that can validate and parse a string that does not have a fixed number of fields.
Code:
Bruce Willis,Richard Gere Code:
^([A-Za-z ]+)([,]([A-Za-z ]+))*$ The current result is (for the second string) Code:
Total match: 'Pink Floyd,Deep Purple,Uriah Heep,Ten Years After,Santana' Code:
Total match: 'Pink Floyd,Deep Purple,Uriah Heep,Ten Years After,Santana' And another question. I use Tcl and Java (just started) and found out that the result of an regexp can be significantly different. Tcl returns 'Pink Floyd' for the match and Java returns 'Santana' when using ([A-Za-z ]+) as a regular expression on the second string. TCL code Code:
set match [regexp $regexp $text matchstr group1 group2 group3 group4 group5 group6 group7 group8 group9 group10] Code:
Pattern p; |
Code:
Pink Floyd,Deep Purple,Uriah Heep,Ten Years After,Santana |
oh my god isn't java UGLY!
ghostdog is right, use split which splits into a tcl list. Code:
#!/usr/bin/env tclsh Code:
$ ./1.tcl < 1 p.s. showing your age with that music ;) |
Yeah - the kids of today ... :p
|
Thanks for the replies. The regular expression implementations in Tcl and Java and possibly in other languages make it possible to parse the data into individual 'blobs'. So why not use it if it's possible? After all, I'm a lazy guy ;)
@bigearsbilly We (or at least I) know you like Tcl and you're not the only one ;) Till now I have managed to write all my applications that needed a GUI in Tcl/Tk. I'm now unfortunately forced to look at Java. |
that's progress (haha)
at least you have a job I guess. I ain't had anything since february :-( |
Sorry to hear about the job. I have one but can't be paid; living on my savings for the last 4 months.
|
The java api can't give a variable number of blobs, see Groups and capturing.
Quote:
Quote:
|
1. regex variations; it is indeed true that different langs/tools often have slightly different regex engines. The best book that explains regexes and differences is here http://regex.info//
2. interesting music :) 3. BB you have my sympathy, had 8 mths out during the GFC :( |
(Part of) the problem is my java implementation; resultTextArea.setText clears the textarea which hides data from previous iterations in the while loop :( I finally figured that out as an 'incorrect' but valid regular expression caused the program to become unresponsive (meaning it ended in an endless loop); so I added a loop counter and with that I only saw the last result.
The revised code Code:
// clear textarea Code:
Loop : 1 I like to thank everybody for their replies. A possibly a useful link: Regular Expression Playground And the lesson learned: what you see is not what you get. |
OOPS, spoke slightly to early. It works as a parser but no longer as a validator :(
|
All times are GMT -5. The time now is 12:03 PM. |