Reading Lines from a String Using a Regular Expression : Serialization « Regular Expressions « Java






Reading Lines from a String Using a Regular Expression


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] argv) throws Exception {
    CharSequence inputStr = "a\rb"; 
    inputStr = "a\r\nb"; 
    inputStr = "a\nb"; 

    String patternStr = "^(.*)$";
    Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(inputStr);
    while (matcher.find()) {
      String lineWithTerminator = matcher.group(0);

      String lineWithoutTerminator = matcher.group(1);
    }
  }
}

 








Related examples in the same category

1.Use FileChannels and ByteBuffers to Store Patterns
2.Using a Regular Expression to Filter Lines from a Reader
3.Apply Regular Expressions on the contents of a file