I find string in word docx, but I want to read next two string. Exapmle: [string id_string, 3, 1000]
I know [string id_string, ... ] and I find this string, with this:
Microsoft.Office.Interop.Word.Range range = Document.Range();
(range.Find.Execute(FindText: "[string id_string, ")
How I can read next two string?
Thanks for your helps!
Regex to the Rescue
This seems like an opportunity for regular expression matching.
Add Imports System.Text.RegularExpressions
at the start of your code to enable use of the Regex class.
Try adding the following code:
Dim docText = range.Text
Const regularExpression As String = "\[string id_string,\s[^\]]+\]"
Dim regex = New Regex(regularExpression)
Dim match = regex.Match(docText)
Dim foundString = match.Value
Assumptions
I'm assuming the following. If my assumptions are incorrect, the answer above may not be quite what you're looking for.
You are using Visual Basic.
If "[string id_string, " is encountered, this absolutely ensures you're at the string you want and there will be a closing bracket to complete the matched set of strings. (This helps keep the regular expression simple, but depending on the content of the text, it could return unexpected results.)
You want the matched [] brackets and all three strings. (This keeps the regular expression simpler than using look ahead/behind to ignore the brackets after pattern-matching.)
You just want the entire list of strings returned in one string, as opposed to the range, line number, position, etc. (You should be able to use the string.Split function to pull out the individual strings if needed.)
You just want the first match you come across. (You can use regex.Matches to get all matches if necessary.)
Reference
For a detailed presentation of using Regex in Word, see the following site from 2008:
http://www.codeproject.com/Articles/26922/Unleashing-the-Full-Power-of-Regular-Expressions-i
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.