• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

regular expression assistance

Hi,
I need help constructing a regular expression for a String.split method in java.

Basically it needs to split around the +, -, /, and * operators. The catch being it needs to include those operators in the output array.

So for example "52+y*10".split(regex) = {"52", "+", "y", "*", "10"}

I have thought about possibly running two split methods and combining the results, but this is a last resort.

I really have no idea how to approach this, any information sources which could help would be appreciated.

Thanks..
0
adam8
Asked:
adam8
  • 2
1 Solution
 
Harisha M GCommented:
In PHP, you can do this:

<?php
    $str = "52+y*10";
    preg_match_all("/\w+|\+|\*|\-|\//",$str,$matches);
    print_r($matches);
?>

There should be something similar in Java...


0
 
adam8Author Commented:
thanks, i'll give it a go.
0
 
adam8Author Commented:
Thanks, this really halped me. I was trying to use the split method, but I didn't even think about checking for matches.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now