Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use java regex to match a line

Tags:

java

regex

The raw data is:

auser1 home1b auser2 home2b auser3 home3b 

I want to match a line, but it doesn't work using ^(.*?)$

However, I can use a(.*?)b to match user1 home1.

How can I match auser1 home1b

like image 753
performanceuser Avatar asked May 26 '11 18:05

performanceuser


People also ask

How do you match a new line in regex?

"\n" matches a newline character.

How do you define the regex string pattern that will match the end of the line?

To match the start or the end of a line, we use the following anchors: Caret (^) matches the position before the first character in the string. Dollar ($) matches the position right after the last character in the string.

How do you match a string in Java?

Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.

What does \\ mean in Java regex?

The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.


1 Answers

By default, ^ and $ match the start- and end-of-input respectively. You'll need to enable MULTI-LINE mode with (?m), which causes ^ and $ to match the start- and end-of-line:

(?m)^.*$ 

The demo:

import java.util.regex.*;  public class Main {     public static void main(String[] args) throws Exception {          String text = "auser1 home1b\n" +                 "auser2 home2b\n" +                 "auser3 home3b";          Matcher m = Pattern.compile("(?m)^.*$").matcher(text);          while (m.find()) {             System.out.println("line = " + m.group());         }     } } 

produces the following output:

line = auser1 home1b line = auser2 home2b line = auser3 home3b

EDIT I

The fact that ^.*$ didn't match anything is because the . by default doesn't match \r and \n. If you enable DOT-ALL with (?s), causing the . to match those as well, you'll see the entire input string being matched:

(?s)^.*$ 

EDIT II

In this case, you mind as well drop the ^ and $ and simply look for the pattern .*. Since . will not match \n, you'll end up with the same matches when looking for (?m)^.*$, as @Kobi rightfully mentioned in the comments.

like image 99
Bart Kiers Avatar answered Oct 04 '22 20:10

Bart Kiers