• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

matching regular expressions

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Intermediate forum...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Consider that '(' is a special character in regular expression patterns, it has a special meaning and function. If you want to refer to the literal character '(', you need to escape it, "\\(". (Of course, you need to escape the escape character '\' as well). Same goes for ')'.

2. You might like to take a look at the "Maximal and Minimal (Greedy and Reluctant) Matching Behavior" section of an introductory article on java.util.regex I wrote a while ago.

3. Think while loop, instead of for loop.
[ August 25, 2004: Message edited by: Dirk Schreckmann ]
 
What's gotten into you? Could it be this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic