Reputation: 879
I have the following string:
"\t Product: ces DEVICE TYPE \nSometext" //between ":" and "ces" are 9 white spaces
I need to parse the part "DEVICE TYPE". I'm trying to do this with Regex. I use this expression, which works.
((?<=\bProduct:)(\W+\w+){3}\b)
this expression returns:
" ces DEVICE TYPE"
The problem is here: Some devices have a string like this:
"\t Product: ces DEVICETYPE \nSometext"
If I use the same expression to parse the device type I get this as result:
" ces DEVICETYPE \nSometext"
How do I get my regex to stop when a \n is found?
Upvotes: 2
Views: 825
Reputation: 11182
You could use:
(?m)((?<=\bProduct:).+)
Explanation:
(?m)((?<=\bProduct:).+)
Match the remainder of the regex with the options: ^ and $ match at line breaks (m) «(?m)»
Match the regular expression below and capture its match into backreference number 1 «((?<=\bProduct:).+)»
Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=\bProduct:)»
Assert position at a word boundary «\b»
Match the characters “Product:” literally «Product:»
Match any single character that is not a line break character «.+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
or
((?<=\bProduct:)[^\r\n]+)
Explanation
((?<=\bProduct:)[^\r\n]+)
Match the regular expression below and capture its match into backreference number 1 «((?<=\bProduct:)[^\r\n]+)»
Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=\bProduct:)»
Assert position at a word boundary «\b»
Match the characters “Product:” literally «Product:»
Match a single character NOT present in the list below «[^\r\n]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
A carriage return character «\r»
A line feed character «\n»
Upvotes: 1
Reputation: 7545
In .NET you can use RegexOptions.Multiline
. This changes the behaviour of ^
and $
.
Rather than meaning the start and end of your string, they now mean start and end of any line within your string.
Regex r = new Regex(@"(?<=\bProduct:).+$", RegexOptions.Multiline);
Upvotes: 2
Reputation: 65059
Perhaps this?
(?<=ces)[^\\n]+
If all you want is what's after ces and before \n that is..
Upvotes: 2