Trying to work on a regular expression that will make this little process easier for me to handle data while giving a rather basic string-based scripting interface. The Intent is to allow a user to affect the interaction of certain objects based on their default interaction. Please note, this may be Minecraft based, but the question is strictly limited to Java as the community for Minecraft coding would be far less valuable.


Code:
// !if 'val' eq 'val' command|elsecommand
String com = "!if '100' == '100' rblk CakeTown 217 67 -22 0|heal Komak57";
String regex = "";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(com);
List<String> matches = new ArrayList<String>();
while (m.find()) {
   matches.add(m.group());
}
msg = "Teleporter: Script command: (" + matches.size() + ") ";
for (int t = 0; t < matches.size(); t++) {
   if (t == 1) {
      msg = msg + "[" + matches.get(t) + "]";
   } else {
      msg = msg + " [" + matches.get(t) + "]";
   }
}

"!if '100' == '100' rblk CakeTown 217 67 -22 0|heal Komak57" should equate to {"!if","100","==","100","rblk CakeTown 217 67 -22 0","heal Komak57"}
"!if" has already been determined and will be constant. the apostrophes suggest variable 1 (which is typically non-spaced, but it's best to allow for spaces just in case) then the eq check, another variable with apostrophes to match boundries, a command which may have any characters except semi-colon, ended by a break character "|" followed by another set of digits containing any characters except a semicolon followed by the end of the string.

"(!if) '(\\.*)' (\\.\\.) '(\\.*)' (\\.*)|(\\.*)" is the best way i can represent it? I just fail horribly at regex =.= help is much appreciated?
Regex is overkill for this. Split on a pipe, split the first of the two arrays on spaces, then re-merge the 5th to Nth items of that array. Voila.
You don't want to build a single regex for the entire thing anyway. You want to start with a basic tokenizer (google it)
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement