?
Solved

regular expression assistance

Posted on 2006-04-02
5
Medium Priority
?
194 Views
Last Modified: 2010-04-17
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
Comment
Question by:adam8
  • 2
3 Comments
 
LVL 37

Accepted Solution

by:
Harisha M G earned 200 total points
ID: 16354091
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
 
LVL 1

Author Comment

by:adam8
ID: 16357440
thanks, i'll give it a go.
0
 
LVL 1

Author Comment

by:adam8
ID: 16357748
Thanks, this really halped me. I was trying to use the split method, but I didn't even think about checking for matches.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question