简体   繁体   中英

How to write Java regular expression to tokenize strings?

I am able to tokenize part of a string, but not the entire string. I need assistance on writing a regular expression to tokenzie the entire string. The string I am trying to tokenzie is 123+56*num1.

The regular expressions below are the ones I have already tried. I can tokenize 123+56*, but not 123+56*num1. It would be great if the expression could match variable names as well.

String variableRegex = "[a-zA-Z_$0-9][a-zA-Z_$0-9]*"; //for variable names
String operatorRegex = "\\+|-|\\/|\\*|\\*|="; // for operators
String multiply = "\\*";
String numberRegex = "\\d*";
String definingVariables = String.format("(%s)(%s)(%s|%s)(%s)", variableRegex, operatorRegex, variableRegex, numberRegex, multiply);
String justVariable = String.format("%s", variableRegex);
String allRegex = String.format("%s|(%s)", definingVariables, justVariable);

The result should be:

123

+

56

*

num 1

The current result is: 123+56*num1

This seems to work:

"[0-9a-z]+|\+|\*"

See https://regex101.com/r/rzhoyx/1/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM