Skip to main content
deleted 31 characters in body
Source Link
Hovercraft Full Of Eels
  • 285.8k
  • 25
  • 268
  • 391

I tried using split but that takes away the delimiter. I need the \n to be kept at the end of each line if it exists.

Then you're using split wrong. You can still use it and preserve the line break if you use look-ahead or look-behind in your regex. Check out the best regular expressions tutorial that I know of:
Regex Tutorial
Look-Around section of the Regex Tutorial.

For example:

public class RegexSplitPageBrk { public static void main(String[] args) { String text = "Hello world\nGoodbye cruel world!\nYeah this works!"; String regex = "(?<=\\n)"; // with look-behind! String[] tokens = text.split(regex); for (String token : tokens) { System.out.print(token); } } } 

The look-ahead or look-behind (also called "look-around") is non-destructive to the characters they match.

I tried using split but that takes away the delimiter. I need the \n to be kept at the end of each line if it exists.

Then you're using split wrong. You can still use it and preserve the line break if you use look-ahead or look-behind in your regex. Check out the best regular expressions tutorial that I know of:
Regex Tutorial
Look-Around section of the Regex Tutorial.

For example:

public class RegexSplitPageBrk { public static void main(String[] args) { String text = "Hello world\nGoodbye cruel world!\nYeah this works!"; String regex = "(?<=\\n)"; // with look-behind! String[] tokens = text.split(regex); for (String token : tokens) { System.out.print(token); } } } 

The look-ahead or look-behind (also called "look-around") is non-destructive to the characters they match.

I tried using split but that takes away the delimiter. I need the \n to be kept at the end of each line if it exists.

You can still use it and preserve the line break if you use look-ahead or look-behind in your regex. Check out the best regular expressions tutorial that I know of:
Regex Tutorial
Look-Around section of the Regex Tutorial.

For example:

public class RegexSplitPageBrk { public static void main(String[] args) { String text = "Hello world\nGoodbye cruel world!\nYeah this works!"; String regex = "(?<=\\n)"; // with look-behind! String[] tokens = text.split(regex); for (String token : tokens) { System.out.print(token); } } } 

The look-ahead or look-behind (also called "look-around") is non-destructive to the characters they match.

added 22 characters in body
Source Link
Hovercraft Full Of Eels
  • 285.8k
  • 25
  • 268
  • 391

I tried using split but that takes away the delimiter. I need the \n to be kept at the end of each line if it exists.

Then you're using split wrong. You can still use it and preserve the line break if you use look-ahead or look-behind in your regex. Check out the best regular expressions tutorial that I know of:  
Regex Tutorial
RegexLook-Around section of the Regex Tutorial.

For example:

public class RegexSplitPageBrk { public static void main(String[] args) { String text = "Hello world\nGoodbye cruel world!\nYeah this works!"; String regex = "(?<=\\n)"; // with look-behind! String[] tokens = text.split(regex); for (String token : tokens) { System.out.print(token); } } } 

The look-ahead or look-behind (also called "look-around") is non-destructive to the characters they match.

I tried using split but that takes away the delimiter. I need the \n to be kept at the end of each line if it exists.

Then you're using split wrong. You can still use it and preserve the line break if you use look-ahead or look-behind in your regex. Check out the best regular expressions tutorial that I know of:  Regex Tutorial.

For example:

public class RegexSplitPageBrk { public static void main(String[] args) { String text = "Hello world\nGoodbye cruel world!\nYeah this works!"; String regex = "(?<=\\n)"; String[] tokens = text.split(regex); for (String token : tokens) { System.out.print(token); } } } 

I tried using split but that takes away the delimiter. I need the \n to be kept at the end of each line if it exists.

Then you're using split wrong. You can still use it and preserve the line break if you use look-ahead or look-behind in your regex. Check out the best regular expressions tutorial that I know of:
Regex Tutorial
Look-Around section of the Regex Tutorial.

For example:

public class RegexSplitPageBrk { public static void main(String[] args) { String text = "Hello world\nGoodbye cruel world!\nYeah this works!"; String regex = "(?<=\\n)"; // with look-behind! String[] tokens = text.split(regex); for (String token : tokens) { System.out.print(token); } } } 

The look-ahead or look-behind (also called "look-around") is non-destructive to the characters they match.

Source Link
Hovercraft Full Of Eels
  • 285.8k
  • 25
  • 268
  • 391

I tried using split but that takes away the delimiter. I need the \n to be kept at the end of each line if it exists.

Then you're using split wrong. You can still use it and preserve the line break if you use look-ahead or look-behind in your regex. Check out the best regular expressions tutorial that I know of: Regex Tutorial.

For example:

public class RegexSplitPageBrk { public static void main(String[] args) { String text = "Hello world\nGoodbye cruel world!\nYeah this works!"; String regex = "(?<=\\n)"; String[] tokens = text.split(regex); for (String token : tokens) { System.out.print(token); } } }