-3

suppose I have a string

  String = ".... helllo.... good \"morning\" .....\" "

I want to get output as

  helllo good morning 

How can I do that using regular expression in Java?

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
Ronin
  • 2,027
  • 8
  • 32
  • 39

5 Answers5

3

If you're just trying to remove the . and the ", then you can do

 str = str.replaceAll("\"|\\.", "");

This regular expression replaces any " (escaped as \" because in a java string literal) or (|) . (escaped first as \. because in a regex then as \\. because a \ must be escaped in a java string literal) by nothing ("").

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

Supposing you just want to maintain the space character and letters, you can use the following regex:

[^a-zA-Z\s]+

If you also want to include numbers:

[^a-zA-Z0-9\s]+

Just replace the matches of that regular expression by an empty string.

Edit:

If you just want to do the opposite (remove certain characters, like . and "), then you can check @dystroy answer.

Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
0

This

String yourString = ".... helllo.... good \"morning\" .....\" ";
System.out.println(yourString.replaceAll("[.\\\"]", ""));

outputs helllo good morning

StepTNT
  • 3,867
  • 7
  • 41
  • 82
0
public static void main(String[] args) {
    String str = ".... helllo.... good \"morning\" .....\" ";
    str = str.replaceAll("[^a-zA-Z]", " ").replaceAll("  +", " ");

    System.out.println(str);
}
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
0

There are many ways to do this.

Here is a way to do it using simple replace methods of String class.

    String s = ".... helllo.... good \"morning\" .....\" ";
    s = s.replace(".","").replace("\"", "");
    System.out.println(s);
John Eipe
  • 10,922
  • 24
  • 72
  • 114