We help IT Professionals succeed at work.

Regular Expression

Manish
Manish asked
on
Medium Priority
341 Views
Last Modified: 2010-03-31
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

Comment
Watch Question

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
be something like:

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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);
      }
}

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

You can also search perl sites as RegExp was a lot history connected to perl.
CERTIFIED EXPERT
Top Expert 2016
Commented:
                 final String RE = "[+\\-=?*_#~^%$]{10,}";
                  String cleaned = s.replaceAll(RE, "<hr>");
try using :-
final String RE = "[[\\+-=\\?\\*_#\\~\\^\\%@\\$]+]+";

as the above is a better Regular Expression.
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.

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);
     }
}
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>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>
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>The following RE is fine as it will look for one or more occurances of the specified symbol

That's not the requirement
okay, maybe english is somehow confusing
"they are continues 10 occurance"
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....
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>for more then 01 occurances....

that should actually be

for 1 or more occurences....
yeah sorry for the typo.....i actually intended to write that only but just missed it :)
ManishLead
CERTIFIED EXPERT

Author

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
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>objects and chej are same and right.

Yes - i'm just filling in the details as sketched out by objects
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.