Remove punctuation from word

I saw a couple of topics here that fit my needs. But no one is specific. If I have a string like "New Delhi" , I want my code to retrieve New Delhi . So, here the quotes are deleted. I want to disable any punctuation, in general, at the beginning and at the end.

So far, this has helped strip punctuation at the end:

String replacedString = replaceable_string.replaceAll("\\p{Punct}*([az]+)\\p{Punct}*", "$1");

What am I doing wrong here? My conclusion "New Delhi with the original quote is still there.

+6
source share
6 answers

The following will remove the punctuation character from the beginning and end of the String object, if any:

 String s = "\"New, Delhi\""; // Output: New, Delhi System.out.println(s.replaceAll("^\\p{Punct}|\\p{Punct}$", "")); 

The ^ part of the regular expression represents the beginning of the text, and $ denotes the end of the text. Thus, ^\p{Punct} will match the punctuation, which is the first character, and \p{Punct}$ will match the punctuation, which is the last character. I used | (OR) to match either the first expression or the second, resulting in ^\p{Punct}|\p{Punct}$ .

If you want to remove all punctuation from the beginning and end of a String object, you can use the following:

 String s = "\"[{New, Delhi}]\""; // Output: New, Delhi System.out.println(s.replaceAll("^\\p{Punct}+|\\p{Punct}+$", "")); 

I just added a + sign after each \p{Punct} . The + sign means "One or more", so it will correspond to many punctuation, if they are present at the beginning or end of the text.

Hope this is what you were looking for :)

+6
source
 class SO { public static void main(String[] args) { String input = "\"New Delhi\""; String output = ""; try { output = input.replaceAll("(^\\p{P}+)(.+)(\\p{P}+$)", "($1)($2)($3)"); } catch (IndexOutOfBoundsException e) { } System.out.println("Input: " + input); System.out.println("Output: " + output); } } 

Result:

 Input: "New Delhi" Output: (")(New Delhi)(") 
+2
source
 String replacedString = replacable_string.replaceAll("^\"|\"$", ""); 

or

 String replacedString = replace_string.replace("\"", ""); 

should also work.

0
source

Try using this:

 String data = "\"New Delhi\""; Pattern pattern = Pattern.compile("[^\\w\\s]*([\\w\\s]+)[^\\w\\s]*"); Matcher matcher = pattern.matcher(data); while (matcher.find()) { // Indicates match is found. Do further processing System.out.println(matcher.group(1)); } 
0
source

to try

 String s = "\"New Deli\"".replaceAll("\\p{Punct}*(\\P{Punct}+)\\p{Punct}*", "$1"); 
0
source

your [az] will only collect lowercase letters and spaces. Try ([a-zA-Z])

-1
source

All Articles