
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pattern UNIX_LINES field in Java with examples
This flag enables Unix lines mode. In the Unix lines mode, only '\n' is used as a line terminator and ‘\r’ is treated as a literal character.
Example 1
import java.util.regex.Matcher; import java.util.regex.Pattern; public class LTERAL_Example { public static void main(String[] args) { String input = "This is the first line\r" + "This is the second line\r" + "This is the third line\r"; //Regular expression to accept date in MM-DD-YYY format String regex = "^T.*e"; //Creating a Pattern object Pattern pattern = Pattern.compile(regex, Pattern.UNIX_LINES); //Creating a Matcher object Matcher matcher = pattern.matcher(input); int count = 0; while(matcher.find()) { count++; System.out.println(matcher.group()); } System.out.println("Number of matches: "+count); } }
Output
This is the first line This is the second line This is the third line Number of matches: 1
Whereas in normal mode \r is treated as carriage-return.
Example 2
import java.util.regex.Matcher; import java.util.regex.Pattern; public class LTERAL_Example { public static void main(String[] args) { String input = "This is the first line\r" + "This is the second line\r" + "This is the third line\r"; //Regular expression to accept date in MM-DD-YYY format String regex = "^T.*e"; //Creating a Pattern object Pattern pattern = Pattern.compile(regex); //Creating a Matcher object Matcher matcher = pattern.matcher(input); int count = 0; while(matcher.find()) { count++; System.out.println(matcher.group()); } System.out.println("Number of matches: "+count); } }
Output
This is the first line Number of matches: 1
- Related Questions & Answers
- Pattern CANON_EQ field in Java with examples
- Pattern CASE_INSENSITIVE field in Java with examples
- Pattern COMMENTS field in Java with examples
- Pattern DOTALL field in Java with examples
- Pattern LITERAL field in Java with examples
- Pattern MULTILINE field in Java with examples
- Pattern UNICODE_CASE field in Java with examples
- Pattern UNICODE_CHARACTER_CLASS field in Java with examples
- Pattern pattern() method in Java with examples
- Pattern compile() method in Java with Examples
- Pattern matcher() method in Java with examples
- Pattern quote() method in Java with examples
- Pattern matches() method in Java with examples
- Pattern split() method in Java with examples
- Pattern splitAsStream() method in Java with examples
Advertisements