• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex Woes

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all,

I'm trying to parse polynomials into their individual terms, e.g. "3x+2" into "3x" and "2", using regular expressions. However, I've no experience with Java regex. The code I've come up with below, after consulting the SDK is



The Match object is returning no match, although I've tested that expression using a 3rd party regex checker and it works. Am I using the Match or Pattern classes incorrectly? Any tips for regex in general are also welcome.

Thanks in advance,
Jim
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well there is an easier solution:

String myPolynomal = "2x+3";

String terms[] = myPolynomal.split("[\+|-|\*|\/]");

+, * are meta characters, and need to be escaped (have a \ in front of them)

"Split myPolynomal on occurances of any in the set [ + OR - OR * OR / ]"
 
Jim Longmore
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the documentation on the Java website, the String class doesn't have a split method, or have I missed something?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Longmore:
According to the documentation on the Java website, the String class doesn't have a split method, or have I missed something?



You've missed the upgrade to Java 1.4
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scheepers, I'm sorry to say your solution has several problems. Within a character class, most of the characters that usually have special meaning, don't. So there's no need to escape the '*', '+', '/' characters--but you should have escaped the '-' because character classes use it to define a range, as in "[a-z]" (but if it's the very first or last character in the set, the hyphen gets treated as a literal character). The '|' shouldn't be in there at all: the OR is implicit in a character class, so the pipe just gets treated a another literal character. Also, when you're creating a regex in a String literal, you have to use two backslashes to escape a character, not one.

Jim, the lookingAt() method means the regex matches the beginning of the target text, but not necessarily all of it. You probably want the find() method, which looks for the next match anywhere with the target. Here's a sample app to demonstrate:
Also, input.substring(match.start(),match.end()) is exactly the same as match.group().
 
Scheepers de Bruin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I stand corrected

(So when is someone building a sample-code-compiler for the ranch to verify?)
=)
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well you will have problems with polynomials
how do you propose to match the constant coefficients with their respective powers of x?
for example
2x^2 + 3x + 5 might also come in as
3x + 2x^2 +5
do you allow only linear polynomials? and are you assured that the order will always be in the decreasing powers of x? and that they will be the most simplified form?
of course these don't come in as regex problems, but just thought I should mention them too.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic