简体   繁体   中英

Extract variable from a simple mathematical equation (Java, String)

I will be handling a bunch of strings that will be of the following format:

"2*salary"
"salary+2"
"2*salary/3"

My goal is to pull out just "salary". I do not however want to eliminate non-characters because I might have something like "2*id3", a mixture of characters and numbers as the variable name (note: it will never be all numbers). I currently use:

Pattern pattern = Pattern.compile("[\\w_]+");

However, for something like "2*salary" this results in "2" and "salary" being found.

You're probably looking for this:

Pattern.compile("[a-zA-Z]\\\\w+");

... in other words, match the sequence of characters that begins with a letter. That'll match 'salary', but won't match '2' (and '2salary' too).

If you in fact do need to match 2salary , use this:

Pattern.compile("[0-9]*[A-Za-z]\\\\w+");

(I have replaced [\\w_] with just \\w , it actually includes underscore).

That is because 2*salary matches twice your "word" character definition \\w which is [a-zA-Z0-9_], the first is 2 and the and match is salary

In your case you need something like "[a-zA-Z][\\w]*"

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