Regular Expression

Hi experts,
  I need a regular expression or any other best way to solve this prob.
I want to replace  
   +,-,=,?,*,_,#,~,^,%,@,$ these special characters with "<hr>" .
It is possible that these characters are in combination (like +-=) or continue one character (++++)
If they are continues 10 occurance then I want to replace it with <hr>
   ex .  ++++++++++++++   .....<hr>
         +-+-+_=$========   ...    <hr>

If possbile , please give me good regular expression tutorial link.

karan

LVL 11
ManishLeadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
be something like:

[\+-=\?\*_#\~\^\%@\$]{n,}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gauravkrtomarCommented:
The following code will definitely help u. This code reaplaces all the occurances of +-=?*_#~^%@$ occuring any number of times and in any combination.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternReplacer {

      public static void main(String[] args) {
            String data = "++++++abcd-----a-+=--";
            Pattern pattern = Pattern.compile("[[\\+-=\\?\\*_#\\~\\^\\%@\\$]+]+");
            Matcher matcher = pattern.matcher(data);
            data = matcher.replaceAll("<hr>");

            System.out.println(data);
      }
}
0
borislavmarkovCommented:

http://www.regular-expressions.info/tutorial.html

You can also search perl sites as RegExp was a lot history connected to perl.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

CEHJCommented:
                 final String RE = "[+\\-=?*_#~^%$]{10,}";
                  String cleaned = s.replaceAll(RE, "<hr>");
0
gauravkrtomarCommented:
try using :-
final String RE = "[[\\+-=\\?\\*_#\\~\\^\\%@\\$]+]+";

as the above is a better Regular Expression.
0
borislavmarkovCommented:
I think post from CEHJ is wrong
>> final String RE = "[+\\-=?*_#~^%$]{10,}";
as it will replace only 10 and above symbols from enumerated.
maybe qualifier "+" will do better
like this:

final String RE = "[+\\-=?*_#~^%$]+";

RegExp is greedy, it will eat symbols as much as it can get.

0
gauravkrtomarCommented:
The following RE is fine as it will look for one or more occurances of the specified symbol:-
final String RE = "[+\\-=?*_#~^%$]+";

But using string.replaceAll(..) is equivalent to using regular expressions, as it internally itself uses the same. So u can use:-

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternReplacer1 {

     public static void main(String[] args) {
          String data = "++++++abcd-----a-+=--";
          Pattern pattern = Pattern.compile("[\\+-=\\?\\*_#\\~\\^\\%@\\$]+");
          Matcher matcher = pattern.matcher(data);
          data = matcher.replaceAll("<hr>");

          System.out.println(data);
     }
}

OR

public class PatternReplacer {

     public static void main(String[] args) {
          final String RE = "[\\+-=\\?\\*_#\\~\\^\\%@\\$]+";
          String data = data.replaceAll(RE, "<hr>");
          System.out.println(data);
     }
}
0
CEHJCommented:
>>as it will replace only 10 and above symbols from enumerated.

Isn't that precisely the requirement?

>>If they are continues 10 occurance then I want to replace it with <hr>
0
CEHJCommented:
>>The following RE is fine as it will look for one or more occurances of the specified symbol

That's not the requirement
0
borislavmarkovCommented:
okay, maybe english is somehow confusing
"they are continues 10 occurance"
0
gauravkrtomarCommented:
Karan,
Please re-state your requirement, as it is a bit ambiguous. What if the occurances are more then 10.

usage:-
final String RE = "[\\+-=\\?\\*_#\\~\\^\\%@\\$]+";            for more then 01 occurances....
final String RE = "[\\+-=\\?\\*_#\\~\\^\\%@\\$]{10,}";       for atleast 10 occurances....
final String RE = "[\\+-=\\?\\*_#\\~\\^\\%@\\$]{10}";        for exact 10 occurances....
final String RE = "[\\+-=\\?\\*_#\\~\\^\\%@\\$]{10,m}";    for atleast 10 but not more than m times occurances....
0
CEHJCommented:
>>for more then 01 occurances....

that should actually be

for 1 or more occurences....
0
gauravkrtomarCommented:
yeah sorry for the typo.....i actually intended to write that only but just missed it :)
0
ManishLeadAuthor Commented:
Thanks ,
  Grt response,
    Ya CEHJ is right,
min occ. should be 10 ie. atleast 10 occ.

I think , objects and chej are same and right.
I havent check others, I will check and let u know.
If possible give more tutorial link.

I will get back to u guys.
Thanks.
karan
0
CEHJCommented:
>>objects and chej are same and right.

Yes - i'm just filling in the details as sketched out by objects
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

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.