tags:

views:

30

answers:

1

I am trying to write a regex in Java to get rid of all heading and tailing punctuation characters except for "-" in a String, however keeping the punctuation within words intact.

  1. I tried to replace the punctuations with "", String regex = "[\\p{Punct}+&&[^-]]"; right now, but it will delete the punctuation within word too.

  2. I also tried to match pattern: String regex = "[(\\w+\\p{Punct}+\\w+)]"; and Matcher.maches() to match a group, but it gives me null for input String word = "#(*&wor(&d#)("

I am wondering what is the right way to deal with Regex group matching in this case

Examples:

Input: @)($&word@)($&                   Output: word
Input: @)($)[email protected]#)(*$&$      Output: [email protected]
+2  A: 
    Pattern p = Pattern.compile("^\\p{Punct}*(.*?)\\p{Punct}*$");
    Matcher m = p.matcher("@)($)[email protected]#)(*$&$");
    if (m.matches()) {
        System.out.println(m.group(1));
    }

To give some more info, the key is to have marks for the beginning and end of the string in the regex (^ and $) and to have the middle part match non-greedily (using *? instead of just *).

Brian Clements