user2598911
user2598911

Reputation: 379

Regex in a line replacement

I have this line in a text file which is in the following format:

/ text
 /

I need to edit the line and remove text and have a result like this:

/ 
 /

What regex should I use to remove the text? I have a problem because one "/" is in the line below.

Upvotes: 0

Views: 84

Answers (3)

Darka
Darka

Reputation: 2768

you can use this regexp if line "/" starts and you don't need anything after it:

String in = "/ text\n /";
String pattern = "^(/)(.+?)(\\n.*)";
System.out.println(in.replaceAll(pattern, "$1$3")); 

Upvotes: 1

David Ramirez
David Ramirez

Reputation: 36

If your trying to remove all characters after a "/" you can do:

String in = "/ text\n /";
String out = in.replaceAll("/.*", "/");

Upvotes: 1

Martijn Courteaux
Martijn Courteaux

Reputation: 68847

How about this?

public String doMagic()
{
    return "/\n /";
}

Upvotes: 4

Related Questions