String and regular expression
Hello,
Assume I have two strings which might contain anything.
Assume I should stroe two strings in one String.
Now I need to parse the composite string and recover the initial two Strings.
I tried to use regular expression but I have some troubles to define the grammer.
Below is my code.
Any help is appreciated.
Pengyou
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SimpleTest {
public static void main(String[] args) {
String part1 = "anything123";
String part2 = "anything456";
String compositeStr = "PART1="+part1+ ";"+"PART2="+part2;
Pattern pattern = Pattern.compile("here I have troubles");
Matcher matcher = pattern.matcher(compositeStr);
int count = 0;
while (matcher.find()) {
count++;
System.out.println("Match number " + count);
System.out.println("value: " + matcher.group());
}
}
}
[979 byte] By [
pengyoua] at [2007-11-27 8:14:56]

How about inserting a seperator char like \n? That's usually no valid input and you could split on it.
> How about inserting a seperator char like \n? That's> usually no valid input and you could split on it.Althouth I can insert any separtor, but I would not use "\n" as I need to display the compositeStr too in a GUI. I don't want to take too many lines.
Well, in that case you can do a lot by merely using two substring/indexOf statements, since you know the semicolon and your prefixed...
Use another delimiter in that case e.g. ?(or a sequence of characters)
kajbja at 2007-7-12 19:59:34 >

What am I missing? Just use split!String[] parts = compositeStr.split(";");
Whatever the delimiter, I still have the problem to find the right regular expression. I would say there are two problems:1. What is the best delimiter which will simplify the parser?2. How to parse it with less coding?
> Whatever the delimiter, I still have the problem to
> find the right regular expression. I would say there
> are two problems:
> 1. What is the best delimiter which will simplify the
> parser?
> 2. How to parse it with less coding?
I obviously don't understand you because, as I said in #5, based on your current code split() is the obvious approach. Are you trying to extract the (name,value) pairs as names and values?
> What am I missing? Just use split!
> > String[] parts = compositeStr.split(";");
It works. But if in the two inital Strings, it also contain a ";", then there will be a problem.
Of course, I should assume that initail two Strings don't contain "PART1" or "PART2".
So your problem is actually "what's a useful separator char".And the real problem is: why are two separate pieces information molded into one String?
> What am I missing? Just use split!> > String[] parts = compositeStr.split(";");D'oh, I didn't see that he already had a delimiter. I agree, and don't see the problem.
kajbja at 2007-7-12 19:59:34 >

> So your problem is actually "what's a useful
> separator char".
>
> And the real problem is: why are two separate pieces
> information molded into one String?
This is because in a web application, I have checkbox for serveal levels of lists (a list of beans which contains another list of beans). When I select a checkbox of the parent list of child list, I need to submit a form in a String[]. So I need to meger different levels into a string first ...
Once we sovle this problem of parser, it will be easier to define another problem ...
> Once we sovle this problem of parser, it will be> easier to define another problem ...I still don't see your problem. It seems to me that you just have to choose a separator that cannot be part of the list items.
> Althouth I can insert any separtor, but I would not use "\n" as I need to display the compositeStr
> too in a GUI. I don't want to take too many lines.
Do you need to merge the two Strings only for this purpose or are the more reasons for doing this?
If not, just use a temporary variable to merge your Strings, display the temp stuff and instead of splitting the merged String continue using the two original Strings.
> > Althouth I can insert any separtor, but I would not
> use "\n" as I need to display the compositeStr
> > too in a GUI. I don't want to take too many lines.
>
> Do you need to merge the two Strings only for this
> purpose or are the more reasons for doing this?
> If not, just use a temporary variable to merge your
> Strings, display the temp stuff and instead of
> splitting the merged String continue using the
> two original Strings.
I will another reason that i will post another issue.
For the moment, I am satisfied with the following:
public class SimpleTest {
public static void main(String[] args) {
String part1 = "anything123";
String part2 = "anything456";
String compositeStr = "PART1=" + part1 + ":::" + "PART2=" + part2;
String[] parts = compositeStr.split(":::");
for (int i = 0; i < parts.length; i++) {
System.out.print("parts[" + i + "]=" + parts+"\n");
}
}
}
Maybe I still need to remove take "PART1" and "PART2" away. I need them to know which part belongs to the first level and which belongs to the second level.
pengyou, please read this: http://forum.java.sun.com/help.jspa?sec=formatting
> Maybe I still need to remove take "PART1" and "PART2"
> away. I need them to know which part belongs to the
> first level and which belongs to the second level.
class Item {
private String firstLevel;
private String secondLevel;
// ... toString 'n stuff
}
You don't even need an additional separator since the 'PARTn=' can act as one
String part1 = "anything123";
String part2 = "anything456";
String compositeStr = "PART1="+part1+ "PART2="+part2;
Pattern pattern = Pattern.compile("(PART\\d+)=(.*?(?=$|PART\\d+=))");
Matcher matcher = pattern.matcher(compositeStr);
int count = 0;
while (matcher.find())
{
count++;
System.out.println("Match number " + count);
System.out.println("whole: " + matcher.group());
System.out.println("name: " + matcher.group(1));
System.out.println("value: " + matcher.group(2));
}