0

I want to match \r\n1.11.1 Entrepreneurship und Unternehmer\r\n

blabla \r\n1.11.1 Entrepreneurship und Unternehmer\r\nEs 

I tried the following RegEx:

\\r\\n\d.\d\d.\d\s+\w\\r\\n

What should it look like?

Link: https://regex101.com/r/QNXniB/1

  • 1
    It is not clear what you're trying to achieve. You want to match every text between `\r\n` or you want to match only text in the form: `{number}.{number}.{number} {text}`? – zhulien Dec 26 '21 at 13:36
  • @zhulien When I extract the text from the pdf, I get `...ntrepreneurship zugesprochen?\r\n1.11.1 Entrepreneurship und Unternehmer\r\nEs gibt keine allgemeingültige Definition für die Begriffe „Entrepreneur“ und „Entrepre-\r\nneurship“. In der Literatur ...` and I try to match all headings. So it should look for `\r\n1.11.1 Entrepreneurship und Unternehmer\r\n` or `\r\n1.1.1 Entrepreneurship und Unternehmer\r\n` or `\r\n1.1 Entrepreneurship und Unternehmer\r\n` or `\r\n1. Entrepreneurship und Unternehmer\r\n` – te191fdsfa Dec 26 '21 at 18:36
  • 1
    Does this answer your question? [Regex for newline "\n\r" and digits](https://stackoverflow.com/questions/70485270/regex-for-newline-n-r-and-digits) – Andrew Morton Dec 26 '21 at 19:51
  • I see this is almost exactly the same question as your previous question [Regex for newline "\n\r" and digits](https://stackoverflow.com/questions/70485270/regex-for-newline-n-r-and-digits). Please respond to the answers there and explain why they do not work. – Andrew Morton Dec 26 '21 at 19:53

2 Answers2

0

The pattern matches a single word character using \w but in the example data there are 1 or more word characters with spaces in between.

Note to escape the dot \. to match it literally.

\\r\\n\d\.\d\d\.\d\s+\w+(?:\s+\w+)*\\r\\n
                     ^^^^^^^^^^^^^^

See a .NET regex demo and a C# demo

If you want to match actual newlines in the text you should not double escape the \\r\\n

var tempAbsatz = "1. Entrepreneurship\r\nAus der Praxis\r\nAndrea  hat  Wirtschaftsingenieurwesen  studiert  und  ist  nun  seit  zwei  Jahren  in  der\r\nLogistik eines größeren mittelständischen Maschinenbauunternehmens tätig. Jörg ist\r\nBetriebswirt mit Schwerpunkt Steuern. Er arbeitet seit Abschluss seines Studiums vor\r\ndrei Jahren für eine große Wirtschaftsprüfungsgesellschaft. Mark, ein Wirtschaftsinfor-\r\nmatiker, führt seit gut zwei Jahren Beratungsprojekte für eine auf Digitalisierung spezia-\r\nlisierte Technologieagentur durch. Nathalie hat vor fünf Jahren das BWL-Studium mit\r\nSchwerpunkt Personal abgeschlossen. Sie leitet seit zwei Jahren die Personalentwick-\r\nlung eines mittelgroßen Elektronikunternehmens.\r\nDie  vier  lernten  sich  im  berufsbegleitenden  MBA-Studium  durch  eine  gemeinsame\r\nGruppenarbeit kennen. Ihre Aufgabe bestand darin, eine innovative Gründungsidee zu\r\nentwickeln. Bei den Vorbereitungen stellten sie fest, dass sie sich in der Verwendung\r\neiniger Begriffe nicht sicher sind. Mal sprechen sie von Unternehmern, mal von Selbst-\r\nständigen, manchmal auch von Entrepreneuren oder Existenzgründern. Antworten auf\r\ndie  Fragen,  welche  Rollen  Entrepreneure  einnehmen  und  warum  Entrepreneurship\r\nvolkswirtschaftlich so wichtig ist, haben sie auch nicht abrufbereit. Insofern wollen sie\r\nzuerst eine Begriffsklärung durchführen.\r\nHierfür recherchieren sie die folgenden Aspekte:\r\n•Was genau versteht man unter Entrepreneurship?\r\n•Lassen sich Unterschiede zwischen Entrepreneuren und Unternehmern finden?\r\n•Gibt es verschiedene unternehmerbezogene Entrepreneurship-Theorien?\r\n•Welche volkswirtschaftliche Signifikanz wird dem Entrepreneurship zugesprochen?\r\n1.11.1 Entrepreneurship und Unternehmer\r\nEs gibt keine allgemeingültige Definition für die Begriffe „Entrepreneur“ und „Entrepre-\r\nneurship“.  In  der  Literatur finden  sich  verschiedene  Ansätze  und  Zugänge  zum  Ver-\r\nständnis dessen, was unter einem Entrepreneur verstanden wird. Oft wird der innova-\r\ntive Charakter des Vorhabens zugrunde gelegt, manchmal auch von Inhabern kleiner\r\nund mittlerer Unternehmen, von selbstständig Tätigen oder von Unternehmensinha-\r\nbern (die auch Mitarbeiter beschäftigen) gesprochen. Eine Differenzierung erfolgt zum\r\nTeil zwischen Personen, die nur einmal gründen, und solchen, die häufiger Unterneh-\r\nmen aufbauen (Parker 2018, S. 6f.). Nachfolgend werden eine typische Begriffsverwen-\r\ndung und einige andere Ansätze skizziert.\r\n12Lektion 1\r\nwww.iubh.de\r\n\r\n";
var h = @"\r\n\d\.\d\d\.\d\s+\w+(?:\s+\w+)*\r\n";
foreach (Match mc in (new Regex(h)).Matches(tempAbsatz)) { 
    Console.WriteLine(mc.Value); 
}

Output

1.11.1 Entrepreneurship und Unternehmer

Looking at the text when there are actual newlines, you might also match the whole line that starts with a digit followed by 1 or more repetitions of a dot and digits:

^\d+(?:\.\d+)+ .*

See a regex 101 demo

Note that in .NET \d can match digits in other languages as well.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Thanks, but I tried your regExpressions 'foreach (Match mc in (new Regex(h)).Matches(tempAbsatz)) { Console.WriteLine(mc.Value); }' but it does not produces any results. – te191fdsfa Dec 26 '21 at 18:30
  • @te191fdsfa If you want to match the newlines in that string, see https://ideone.com/PD3r7U I have updated the answer. – The fourth bird Dec 26 '21 at 18:52
  • Ok, it should match only to the first `\r\n`, like `\r\n1.21.2 Unternehmerbezogene Theorien des\r\n` and not `\r\n1.21.2 Unternehmerbezogene Theorien des\r\nEntrepreneurships\r\n`. Is there a fix? – te191fdsfa Dec 26 '21 at 19:15
  • @te191fdsfa It matches the expected result in the question. If you want only a single match, you can use [Regex.Match](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.match?view=net-6.0) instead. If you only want a match if there is an empty line following: `\r?\n\d\.\d\d\.\d\s+\w+(?:\s+\w+)*\r?\n(?=$)` See https://regex101.com/r/VhdtWm/1 – The fourth bird Dec 26 '21 at 19:35
0

This should handle it:

(?:(?:\\r\\n)|^)(?<index>[0-9]+\.(?:[0-9]+\.?)*)(?<title>.+?)(?:(?:\\r\\n)|$)

It looks more complex than it actually is. Most of it is for better usability. The core of it is actually:

(\\r\\n) (\d+\.(\d+\.?)*) (.+?) (\\r\\n).

Where:

  1. (\\r\\n) - search for the literal string "\r\n".
  2. (\d+\.(\d+\.?)*) - match a number followed by a . at least once optionally followed by 0 or more occurrences of another number (followed or not by a .). Valid cases: 1., 1.1, 1.1., 1.1.1.3.4). This will match the index.
  3. (.+?) matches any non-newline character 1 or more times or in other words any possible sequence of 1+ characters (including just white space). This will match the title and it can be improved based on your needs.

As there is a disparity between the titles format {index}. (ending with a . as in "1. Entrepreneurship") vs {index}.{subindex} (not ending with a . as in "1.11.1 Entrepreneurship und Unternehmer") we're going to need to check for both.

The regex will match all headings starting with an index in the form <index>. + 0 or more sub-indexes in the form <subindex>. or <subindex> (i.e {index}.{subindex}.{subindex} -> 2.14.3).

Check the results here.

You also need to handle the condition of the string start and end where we won't have a "\r\n" sequence. This is done through (?:(?:\\r\\n)|^) and (?:(?:\\r\\n)|$) respectively or in simple words - search for occurrences which either start/end with \r\n or are at the start/end of the text itself.

On each match with the given regex, you can easily access the values of the matched index and title directly by the named groups index and title.

string text = "<your-text>";
Regex regex = new Regex("(?:(?:\\r\\n)|^)(?<index>[0-9]+\.(?:[0-9]+\.?)*)(?<title>.+?)(?:(?:\\r\\n)|$)", RegexOptions.Singleline);

var match = regex.Match(text);

if (match.Success)
{
    var index = match.Groups["index"].Value;
    var title = match.Groups["title"].Value.Trim();
}
zhulien
  • 5,145
  • 3
  • 22
  • 36
  • Sounds good. But it still matches `107.000 Euro für technologieorientierte Dienstleistungsbranchen, 113.000 Euro bei den` or `9.500 und 12.400 Privatinvestoren tätig waren, davon ein Großteil als Business Angels`. What would be the fix here? – te191fdsfa Dec 28 '21 at 13:40
  • You haven't specified this case in the example text. Are those preceded by `\r\n`? Please, update the example text we need to test over by included such cases. – zhulien Dec 28 '21 at 14:02