Stack Overflow Asked by OvenBakedPython on December 18, 2021
I have a tab delimited text file containing comments/notes.
Any line that contains the the string "read" or "reading" will/should have an 8 digit number that I want to extract. The problem is I don’t know where in the line the 8 digit number could be. As these are user generated comments each line could be different.
Example Line from text file:
3253442999934 did not read book # 98713429
what I have tried:
var m = Regex.Match(realLine, @".*""reading"":""\b(?x)[0 - 9]{8}\b$"".*");
var n = Regex.Match(realLine, @".*""read"":""\b(?x)[0 - 9]{8}\b$"".*");
//string pattern = "bd{8}b$";
Regex regex = new Regex(pattern);
console.WriteLine((m.Groups[1].Value));
console.WriteLine((n.Groups[1].Value));
I’m not generating any result, meaning nothing is printed to the console.
You may use
var result = Regex.Match(realLine, @"bread(?:ing)?b.*?#s*(d+)")?.Groups[1].Value;
See the regex demo
Regex details
b
- a word boundaryread(?:ing)?
- read
or reading
b
- a word boundary.*?
- zero or more chars other than a newline char, as few as possible#
- a #
chars*
- zero or more whitespaces(d+)
- Group 1: one or more digits.To actually return Group 1 value, Regex.Match(realLine, @"bread(?:ing)?b.*?#s*(d+)")?
is used, and if it does not match, an empty string will be returned, else, .Groups[1].Value
of the current match object will be returned.
Answered by Wiktor Stribiżew on December 18, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP