2

I have a body of text and I am trying to find a character sequence within it. String.contains() will not work so Im trying to use String.matches method and a regular expression. My current regex isn't working. Here are my attempts:

"1stline\r\n2ndline".matches("(?im)^1stline$"); 
// returns false; I expect true

"1stline\r\n2ndline".matches("(?im)^1stline$") 
// returns false

"1stline\r\n2ndline\r\n3rdline".matches("(?im)^2ndline$")   

"1stline\n2ndline\n3rdline".matches("(?im)^2ndline$")

"1stline\n2ndline\n3rdline".matches("(?id)^2ndline$")

How should i format my regex so that it returns true?

MERose
  • 4,048
  • 7
  • 53
  • 79
Arvin
  • 1,210
  • 2
  • 15
  • 18

3 Answers3

14

You need to use the s flag (not the m flag).

It's called the DOTALL option.

This works for me:

  String input = "1stline\n2ndLINE\n3rdline";
  boolean b = input.matches("(?is).*2ndline.*");

I found it here.

Note you must use .* before and after the regex if you want to use String.matches().

That's because String.matches() attempts to match the entire string with the pattern.

(.* means zero or more of any character when used in a regex)


Another approach, found here:

  String input = "1stline\n2ndLINE\n3rdline";
  Pattern p = Pattern.compile("(?i)2ndline", Pattern.DOTALL);
  Matcher m = p.matcher(input);
  boolean b = m.find();
  print("match found: " + b);

I found it by googling "java regex multiline" and clicking the first result.

(it's almost as if that answer was written just for you...)

There's a ton of info about patterns and regexes here.


If you want to match only if 2ndline appears at the beginning of a line, do this:

   boolean b = input.matches("(?is).*\\n2ndline.*");

Or this:

 Pattern p = Pattern.compile("(?i)\\n2ndline", Pattern.DOTALL);
Community
  • 1
  • 1
jahroy
  • 22,322
  • 9
  • 59
  • 108
0

If you are trying to see if "1stLine" exists within the string "1stline\r\n2ndline\r\n3rdLine" then you could just do the following:

String test1= "1stline\r\n2ndline\r\n3rdLine";
System.out.println(test1.contains("1stline"));
//returns true

----------------------------EDIT----------------------------

So for some reason .contains wont work in your test. Here is the regular expression to find the string "1stline"

    String regex = "([\\r\\n]|.)*(1stline)+([\\r\\n]|.)*";

    String test1= "1stline\r\n2ndline\r\n3rdLine";
    System.out.println(test1.contains("1stline"));
    //returns true

    String test2 = "1stline\r\n2ndline\r\n3rdLine";
    System.out.println(test2.matches(regex));
    //returns true

    String test3 = "1stline\r\n";
    System.out.println(test3.matches(regex));
    //returns true

    String test4 = "1stline";
    System.out.println(test4.matches(regex));
    //returns true

    String test5 = "\r\n2ndline\r\n3rdLine";
    System.out.println(test5.matches(regex));
    //returns false

    String test6 = "n2ndlinen2ndlinen2ndline\r\n2ndline\r\n4rdLine";
    System.out.println(test6.matches(regex));
    //returns false
wdziemia
  • 1,429
  • 1
  • 14
  • 19
  • thanx for you answer. But just to make it clear, Im looking for a word between `\r\n`'s in a multiline string. Im not looking for the `\r\n` itself – Arvin Jul 24 '13 at 03:27
  • In other words, you want to return the words between "\r\n" ? – wdziemia Jul 24 '13 at 03:30
  • My bad. I mean I want to know if such word exists. – Arvin Jul 24 '13 at 03:31
  • So if the string "1stLine" exists on any line? – wdziemia Jul 24 '13 at 03:49
  • @Arvin I edited my question. Take a Look. – wdziemia Jul 24 '13 at 03:54
  • Ok, my bad again. I want to know if the line exist. Said line could be the 1st line or the last line or anywhere inside the string. `String.contains` just wouldn't do it. thanx for your patience. – Arvin Jul 24 '13 at 04:40
  • @Arvin edited once more. Take a look – wdziemia Jul 24 '13 at 05:01
  • 1
    Rather than build a complicated regex with platform-dependant newline characters, you can just use `(?s)` to enable [`DOTALL mode`](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#DOTALL). Example [here](http://stackoverflow.com/a/17825571/778118). – jahroy Jul 24 '13 at 05:54
0

You could try using contains

It would be something like

yourString.contains("(.+\\r\\n)+(.)+(wordYouAreLookingFor)");

btw, using same example that @wdziemia, I'm not expert at REGEX either... :)

hope it helps.

unmultimedio
  • 1,224
  • 2
  • 13
  • 39