Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am not exactly sure what you are really trying to accomplish with this regex but the following code will match the the string 'keyword' when it has white space on either side of it: </p> <pre><code>string resultString = null; try { Regex regexObj = new Regex(@"\b(keyword)\b"); resultString = regexObj.Match(subjectString).Value; } catch (ArgumentException ex) { // Syntax error in the regular expression } </code></pre> <p>It can be generally explained as: the \b asserts the position at the beginning and end word boundaries. In this case I assumed the word of interest was keyword.</p> <p>I also thought from my interpretation of your question that you might be interested in matching the entire series of characters that follow the keyword up to the line break. If that is the case the following regex code will return that match: </p> <pre><code>string resultString = null; try { Regex regexObj = new Regex(@"\bkeyword\b(\w*\s*)$"); resultString = regexObj.Match(subjectString).Value; } catch (ArgumentException ex) { // Syntax error in the regular expression } </code></pre> <p>This regular expression can be interpreted as find the beginning and ending word boundaries which is the reason for the \b on either side. The (\w*\s*)<em>$ reads like this match all word \w</em> characters and space characters \s* as many times as they occur and move position to the end of the line $. </p> <p>This next bit of code will read in the entire line of data that contains the keyword, lines of data that do not contain the keyword will not match.</p> <pre><code>string resultString = null; try { Regex regexObj = new Regex("^.*keyword.*$"); resultString = regexObj.Match(subjectString).Value; } catch (ArgumentException ex) { // Syntax error in the regular expression } </code></pre> <p>Explained: the ^ positions at the beginning of the string, the .* matches any character that is not a line break character, the keyword is then included followed by the .* so the remaining non line breaking characters are included and the $ asserts the position at the end of the string which would be the entire line in this example.</p> <p>I hope the above is helpful if not this time maybe in the future. I am always trying to discover alternative practices to achieve the same results, so if you have any constructive criticism please post it. </p> <p>Best wishes, Steve</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload