import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?s)^(.*?)\\*\\*\\*\\*\\*\\*\\*";
final String string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus accumsan risus id ex dapibus sodales. \n\n"
+ "Curabitur dui lacus, tincidunt vel ligula quis, volutpat mattis eros. \n\n"
+ "In quis metus at ex auctor lobortis. Aliquam sed nisi purus. Sed cursus odio erat, ut tristique sapien interdum interdum. Morbi vel sollicitudin ante, non pellentesque libero. \n\n"
+ "***********\n\n"
+ "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Aenean egestas urna facilisis massa posuere, quis accumsan erat ornare. \n\n"
+ "Curabitur at dapibus nibh. Nam nec vestibulum ligula. Phasellus bibendum mi urna, ac hendrerit libero interdum non. Suspendisse semper non elit aliquam auctor. \n\n"
+ "Morbi vel sem tortor. Donec a sapien quis erat condimentum consequat in ut sem. Quisque in tellus sed est lobortis ultricies sed vitae enim.\n"
+ "I want to return this value in B1:\n\n"
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus accumsan risus id ex dapibus sodales. \n\n"
+ "Curabitur dui lacus, tincidunt vel ligula quis, volutpat mattis eros. \n\n"
+ "In quis metus at ex auctor lobortis. Aliquam sed nisi purus. Sed cursus odio erat, ut tristique sapien interdum interdum. Morbi vel sollicitudin ante, non pellentesque libero. \n"
+ "Which is basically anything before the pattern *******. In Python, I can add the re.DOTALL to the .* but I can't get this to work in Google Sheets.\n\n"
+ "regexgoogle-spreadsheet\n"
+ "shareeditcloseflag\n"
+ "asked 3 hours ago\n\n"
+ "aylim14\n"
+ "83\n"
+ "Try using stackoverflow.com/a/159140/5267751. – user202729 3 hours ago\n"
+ "Thanks! I did. But it didn't work. That was one of the first thing I tried. It kept returning everything and didn't stop at the *. The suggestion gave by Poul looks like it worked. But I can't say for sure until new data gets added to the spreadsheet. – aylim14 1 hour ago \n"
+ "add a comment\n"
+ "2 Answers\n"
+ "active oldest votes\n"
+ "up vote\n"
+ "0\n"
+ "down vote\n"
+ "This simple RegEx should do the trick (although I don't know anything about Google-spreadsheets):\n\n"
+ "[^*]*(?=\\*{11})\n"
+ "The first match should be what you want. It simply matches anything up the the first '*', making sure it's followed by 11 stars.\n\n"
+ "shareeditflag\n"
+ "edited 3 hours ago\n"
+ "answered 3 hours ago\n\n"
+ "Poul Bak\n"
+ "1,680519\n"
+ "The *{11} actually doesn't matter. Sometimes, the separator is *****. Sometimes it's *\\n*\\n*\\n* So I really just need it to stop at the first *. – aylim14 1 hour ago \n"
+ "I just substituted the [^*]* and I think it worked! Thanks so much. The spreadsheet gets updated once or twice a day.I'll monitor this to see if it works on new data. – aylim14 1 hour ago\n"
+ "add a comment\n"
+ "up vote\n"
+ "0\n"
+ "down vote\n"
+ "To make a dot match line breaks, you need to add (?s) to the pattern. To match any char, you may use a .. To match up to the leftmost occurrence, use lazy quantifier, *?. To actually extract a substring you need, wrap the part of the pattern you are interested in getting with capturing parentheses.\n\n"
+ "So, to match up to the first ******* substring, you may use\n\n"
+ "(?s)^(.*?)\\*\\*\\*\\*\\*\\*\\*\n"
+ "or (?s)^(.*?)\\*{7}.\n\n"
+ "(?s) - a DOTALL modifier\n"
+ "^ - start of string\n"
+ "(.*?) - Group 1: any 0+ chars as few as possible\n"
+ "\\*\\*\\*\\*\\*\\*\\* - 7 literal asterisk symbols.\n"
+ "Note you cannot rely on a negated character class (that matches line breaks) if your substring may contain * chars, that is, ^([^*]*)\\*\\*\\*\\*\\*\\*\\* won't work in those cases.\n\n"
+ "If you just want to match any chars up to the first * in the string, your regex will simplify greatly to\n\n"
+ "^([^*]+)\n"
+ "It matches\n\n"
+ "^ - start of string\n"
+ "([^*]+) - Capturing group 1: one or more chars other than *.\n"
+ "shareeditdeleteflag\n"
+ "answered just now\n\n"
+ "Wiktor Stribiżew\n"
+ "284k16113183\n"
+ "add a comment\n"
+ "Not the answer you're looking for? Browse other questions tagged regex google-spreadsheet or ask your own question.\n"
+ "asked\n\n"
+ "today\n\n"
+ "viewed\n\n"
+ "15 times\n\n"
+ "active\n\n"
+ "today\n\n"
+ "BLOG\n"
+ "Developer Salaries in 2018: Updating the Stack Overflow Salary Calculator\n"
+ "HOT META POSTS\n"
+ "33 Generic “Don't Do It” Answer\n"
+ "30 What effect do voting rings have on average post quality?\n"
+ "58 C tag usage, radical changes to tag wiki. Which policy to keep?\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html