#1
|
||||
|
||||
How to find all string within string.
I am using VBscript, FSO for opening the file and do some replacements. After that I get strtxt as string having file contents. Now I want to find lines containing specific srtring.
The question I searched very vigorously but could not find the answer. I got little bit of help from the below mentioned link but I think should ask here to check weather this possible or not. This is cross post from Here: vbscript - How to extract lines from String - Stack Overflow |
#2
|
||||
|
||||
You haven't given enough details to work with; it's not even apparent why you're using vbscript or FSO, or what you want to do with the string you're looking for. For code to Find a string within a defined range, see, for example:
https://www.msofficeforums.com/word-...le-column.html https://www.msofficeforums.com/word-...nces-text.html
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
I want to make multiple regex find and replace in the rtf text so to extract some information. as I want to play with text and not the rtf as opened in word I am using fso.
This is the code sniplet: Code:
Const ForReading = 1, ForWriting = 2, ForAppending = 8 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Dim fso, FileName, TextLine, Tempfile, inTempfile, strPath, Temp, intemp FileName = "C:\Users\rahul\Desktop\PLR\1.rtf" TempFile = "C:\Users\rahul\Desktop\PLR\Temp\1.txt" Set fso = CreateObject("Scripting.FileSystemObject") Set inFile = fso.OpenTextFile(FileName) Do While inFile.AtEndOfStream <> True TextLine = inFile.ReadAll Loop Set regEx_ = new regExp With regEx_ .Global = True .MultiLine = True .IgnoreCase = True .Pattern = "\{\\\w{4}\\\w{4}\\\w{11}" TextLine = regEx_.replace(TextLine, "mmrk$&") 'many such find and replace to get IMP lines Having mmrk End with set Temp = fso.CreateTextFile(TempFile, True) Temp.Write TextLine Temp.Close Set Temp = Nothing Set intemp = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Users\rahul\Desktop\PLR\Temp\1.txt") Set outFile = CreateObject("Scripting.FileSystemObject").CreateTextFile("C:\Users\rahul\Desktop\PLR\Temp\11.txt",True) Do Until intemp.AtEndOfStream Dim line : line = intemp.Readline If Left(line, 4) = "mmrk" then outFile.writeLine(line) Loop intemp.Close outFile.Close inFile.Close If you see the code, You will notice that I am saving the string first as file and opening the same for reading again. Is there anyway we can extract lines containing specific string from string so as to avoid it saving. |
#4
|
||||
|
||||
Regardless of whether you want to "want to play with text and not the rtf as opened in word", that doesn't mean you can't open the file as text in Word and process it as such. For example:
Code:
Sub Demo() Dim wdDoc As Document Set wdDoc = Documents.Open(FileName:="C:\Users\rahul\Desktop\PLR\1.rtf, Format:=wdOpenFormatText, AddToRecentFiles:=False) With wdDoc 'process here .Close SaveChanges:=True End With End Sub Code:
Sub EditRTFFile() 'Requires a reference to Microsoft VBScript Regular Expressions 5.5 Dim DataFile As String, TextLine As String, Data As String, regEx DataFile = "C:\Users\rahul\Desktop\PLR\1.rtf" Set regEx = CreateObject("vbScript.RegExp") Data = "" Open DataFile For Input As #1 Do Until EOF(1) Line Input #1, TextLine On Error Resume Next With regEx_ .Global = True .MultiLine = True .IgnoreCase = True .Pattern = "\{\\\w{4}\\\w{4}\\\w{11}" TextLine = regEx_.Replace(TextLine, "mmrk$&") End With Data = Data & TextLine & vbCrLf Loop Close #1 Open DataFile For Output As #1 Print #1, Data Close #1 Set regEx = Nothing End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
||||
|
||||
Problem is not in opening file, reading it line by line and doing regex. Real problem starts after that.
For example I am getting by any means, StrTxt as string which contains multiple lines or I don't know how string behaves inside object. As far as my problem concerns I get StrTxt from two souces. 1. by opening a file. 2. from httprequests. In both the case I end up with StrTxt as string. So the question is, If My StrTxt contains multiple occurrences of a string like "string", how to get all of it. For example in hypothetical string. Code:
StrTxt = "mmrkxyz" & vbCr & "mmrkyzx" & vbCr & "xyz" & vbCr & "yzx" & vbCr & "mmarkxxx" Code:
StrTxt = "mmrkxyz" & vbCr & "mmrkyzx" & vbCr & "mmarkxxx" To make it simple, My strings contains multiple lines. I just want it to reduced to lines that contains specific characteristics like Left(StrTxt, 4) = "mmrk". |
#6
|
||||
|
||||
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
||||
|
||||
That Is what I am currently doing sir. Have no problem with That but thought if withing VBA it is possible It would be great for knowledge.
|
#8
|
||||
|
||||
So, since you agree it can be done with Find/Replace, why don't you do that with VBA?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
||||
|
||||
Because I get the text as string from httprequest's response text. I then insert that text to instance of new word doc (temp), then extract the text and close the word unsaved. This works OK, but thought If it can be directly done within VBA what is the need to open new instance of word un-necessarily.
|
#10
|
||||
|
||||
Nothing in what I have said implies starting another Word session. Furthermore, everything I have suggested envisages the use of VBA. Indeed, all the code I have posted is VBA, not the vbscript you've been trying to use!!! I'd have thought that much, at least, was obvious.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
||||
|
||||
My Mistake. I will try and then comment further.
|
#12
|
||||
|
||||
Find sub-strings in String.
Earlier I was doing things differently but after this answer https://www.msofficeforums.com/word-...ng-string.html I get an Idea that is outlined here.
I am getting StrTxt as string after httpreq response text. Now I want to find each Substring in StrTxt using regex. Code:
Set re = New RegExp re.Pattern = "pattern" re.Global = True re.IgnoreCase = True 'if required For Each m In re.Execute(StrTxt) Msgbox m 'run another function for each substring. Next Last edited by PRA007; 02-10-2016 at 10:55 AM. |
#13
|
||||
|
||||
PRA007, Please don't start multiple threads on the same topic. I've merged them.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
||||
|
||||
Okay. My first question for file and extract line containing the string.
This question is about how to find Strings within a string and not the line So I thought that might be different though the solution will be almost similar. |
#15
|
||||
|
||||
As I've said, that can be done with Word's Find/Replace. There have been posts discussing that on these forums (e.g. https://www.msofficeforums.com/word-...html#post95275). Or it can even be done with Regex within Word.
But, until you tell us what the strings and substrings are, it's a bit much expecting us to tell you how to do it if it entails the use of wildcards, especially.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
vbscript, word vba |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Wildcard replace any string in context with a specified string | wardw | Word | 7 | 05-07-2018 09:13 AM |
How can I compare a string in a cell to another string? | Amitti | Word VBA | 2 | 04-10-2017 07:35 PM |
Why is this Find string not working | TechEd | Word VBA | 5 | 07-05-2014 08:12 PM |
Way to search for a string in text file, pull out everything until another string? | omahadivision | Excel Programming | 12 | 11-23-2013 12:10 PM |
Bad view when using Find and Find & Replace - Word places found string on top line | paulkaye | Word | 4 | 12-06-2011 11:05 PM |