Is there just an easy (literal) way to create a begin and end reference point for text I wish to capture? I have a script that I use within my $profile, but for the life of me the method of replacing/removing text with REGEX on a multiline document is something I cannot figure out. My code:
function def($searchterm){
$response= @((wget "https://google.com/search?q=definition $searchterm").allelements|where outertext -match "$searchterm")
[string]$response = @($response.innertext)|select -skip 13 # This is the number of lines of text at the beginning of each search result
New-textbox -top 45 -left 400 -width 1200 -height 600 -text $response -show -TextWrapping WrapWithOverflow
}
while the first section is easy in this regard, I struggle wiping the end of it. For example, searching for the word Help net a long definition (redacted for readability)
help [verb] ... ... "Help! I'm drowning!" used as an appeal for urgent assistance. "Help! I'm drowning!" "Help! I'm drowning!" "Help! I'm drowning!" "Help! I'm drowning!" People also ask
What is the true meaning of help? ... no less than 80 more lines
While I was typing this out, I considered running the select
method again and did
$response|select -skiplast 80
This understandably gave me a blank textbox. Then I realized I could set the parameters from the front end by using the -FIRST switch after the skip. That worked! However, 31 is the number for the word "help". What if one searched for the word "asterisk" which has much fewer meanings?
In conclusion, I can just ignore the stuff at the end, but every response includes the "People also ask". I cannot seem to get my head around the RegEx but I tried $response.replace("^People also ask.*$","")
but this does nothing. I also tried running it like a split, but that did not work either.
OP Edit: This is the result I am getting. What you do not see is another 80 to 100 lines below the bottom of the window. I want a way to remove "People also ask" and everything after it.
CodePudding user response:
Your question is difficult to follow, but if you just want to remove "People also ask" and everything after it then you could use, for example
$result = $response -replace "(?s)People also ask.*", ""
where (?s)
turns on single-line mode so that .
matches across newlines.