| | |
matching regular expressions
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2004
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Regular Expressions with Decimal Points (Java)
- Regular Expressions (VB.NET)
- Parsing a log file using regular expressions (C#)
- regular expressions (C#)
- PHP4 regular expressions? (PHP)
- Regular Expressions (C#)
- Regular Expressions (Computer Science)
- Regular Expressions (C#)
- mod_rewrite: help with regular expressions (Linux Servers and Apache)
Other Threads in the Java Forum
- Previous Thread: Saving a file
- Next Thread: Help with program...Please......
Views: 3028 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider detection draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle os plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree windows






