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

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

0
Manish
Asked:
Manish
  • 6
  • 5
  • 3
  • +2
3 Solutions
 
objectsCommented:
be something like:

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

0
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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