User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 402,860 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,908 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2367 | Replies: 1
Reply
Join Date: Jul 2004
Posts: 6
Reputation: macca1979 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
macca1979 macca1979 is offline Offline
Newbie Poster

matching regular expressions

  #1  
Aug 25th, 2004
Hi there,

First off I'm new to java and coding in general so please bear with me. I'm trying to write a bit of code that will convert something like the following
con(pk(B),pk(AS),pk(A),B)
to this
k_B, k_AS, k_A, B

I have written the following code :

Pattern pk = Pattern.compile("pk(.*)"); //Regular Expression Pattern

int varsLen = initialVars.length();
String[] elements = initialVars.split(","); //Split up the terms
for (int x=0; x<elements.length; x++) {
Matcher m = pk.matcher(elements[x]); //match array element
boolean b = m.matches(); //Does array element match
if (b) { //If there is a match
pkStart = elements[x].indexOf("pk(", pkEnd) + "pk(".length();
pkEnd = elements[x].indexOf(")",pkStart);
elements[x] = elements[x].substring(pkStart,pkEnd);
csPrelimOut = "k_"+elements[x];
csOut = csOut.concat(csPrelimOut);
} else {
csPrelimOut = elements[x];
csOut = csOut.concat(csPrelimOut);
}

}
csOut = csOut.replace(' ',',');

When I use this code it always works for the first element of the array but not the remaining elements. I'd get k_B from pk(B), but for the remaining pk() elements i'd just get them reiterated e.g. pk(AS) would remain pk(AS). I've never used regular expressions before so maybe that's where I'm going wrong. From "Pattern pk = Pattern.compile("pk(.*)");", I mean to set up a regular expression that starts with "pk(", then can be followed by 1 or more characters, and finally ended with ")". Does anyone have any idea how I might sort this out? Thanks a million.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2004
Location: Pakistan
Posts: 1,673
Reputation: nanosani is an unknown quantity at this point 
Rep Power: 8
Solved Threads: 49
Colleague
nanosani's Avatar
nanosani nanosani is offline Offline
Unauthenticated Liar

Re: matching regular expressions

  #2  
Aug 26th, 2004
I think you should use the string tokenizer for the following expression .. it'll be much easier for you to code.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 1:25 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC