Wrox Programmer Forums
|
BOOK: Beginning Regular Expressions
This is the forum to discuss the Wrox book Beginning Regular Expressions by Andrew Watt; ISBN: 9780764574894
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Regular Expressions section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 5th, 2008, 10:57 AM
Registered User
 
Join Date: Aug 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Repetitive regex

Hey folks,

            Is it possible to have a regex that will parse out groups of characters from the following example. The '|' characters are part of th e text, if anyone's familiar with Fitnesse they'll recognise the syntax


| This | a | is | b | the | c | full | d | method | e | name |

basically the line is delimited using the '|' character and every odd token i.e. This, is, the, full, method, name makes up the whole method name. Thisisthefullmethodname.

Is it possible to write a regular expression that will parse out these values? Even to provide a Java pattern that will allow me to check each odd group?

I've tried the following but to no avail.

(\|[^\|]*?)*

Thanks,
Mark.
 
Old August 5th, 2008, 01:18 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Well, there's a trivial non-regex way to do this in Java.

String text = "|This|a|is|b|demo|"
String[] chunks = text.split("\\|");
StringBuffer result = new StringBuffer(text.length());
for ( int c = 1; c < chunks.length(); c+=2 ) // chunk 0 has no content
{
    result.append( chunks[c] );
}
String answer = result.toString();

No?

(Actually, there is a regex in there...the argument to split.)

But if you have some real reason to do this all via a single regex...Ehhh...I don't see one, but it's a fun challenge.
 
Old August 6th, 2008, 03:31 AM
Registered User
 
Join Date: Aug 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:
Ehhh...I don't see one, but it's a fun challenge.
Hey Old Pedant,
         Exactly pal. I realise it could be done fairly handily
with java but one regex to rule them all??? Now yer talking.
If ye know of any decent discussions of grouping and backreferences
that would be great.

Thanks,
Mark.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex pendemv JSP Basics 4 December 8th, 2008 08:29 AM
repetitive xml - sorry, I'm new to this... JamesHurrell XSLT 4 March 12th, 2008 04:24 PM
repetitive instruction, more efficient solution? stolte XSLT 4 November 27th, 2007 02:41 PM
XSLT Table Row with repetitive first cell the cause XSLT 5 July 3rd, 2007 01:29 PM
Regex htran XSLT 2 May 18th, 2005 09:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.