Extract the 7 words before every '$' that appears and the 7 words after it (or until period) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
samt
Posts: 62
Joined: 09 Sep 2022, 13:29

Extract the 7 words before every '$' that appears and the 7 words after it (or until period)

Post by samt » 26 Feb 2023, 21:35

I imagine I would need an array for this, which I have set up already that Mikeyww gave that I should be able to use this regex with.

As title says, trying to get 2 different regex codes:

1 code) Extract the 7 words before and after every '$' in a string. (include the # attached to each '$")
2nd code) Extract the 7 words before every '$', and all the words after the '$' leading up to a period. (include the # attached to each '$")
None of these below have worked. (found them around and tweaked).

Code: Select all

    
    ((?:\p{Xwd}+\P{Xwd}){0,7})\$((?:\P{Xwd}\p{Xwd}+){0,7})
    
    ([a-zA-z\s+){0,7}\$([a-zA-z\s+){0,7}
    
    ([^ ]+ ){0,7}\$( [^ ]+){0,7}
3 issues with the code above,

1) the code only detects the FIRST '$' in the string even when string has multiple '$' that should be detected
2) it only gets the words before that first '$', never any words after it.
3) When I implement them into autohotkey for an array, the msgboxes that appear have 1 less character (from the far left) than the previous msgbox, not sure why that is. So with each new msgbox that pops up, it's the same thing as previous msgbox but 1 character from the far left is deleted.

Here's what it all looks like in my AHK.:

Code: Select all

 numm := []
    test := so this is just a test ok testing $5 dollars for that ice-cream sandwhich is not bad at all. And also another sentence to test $10 thousand for an ice-cream sand which ain't bad either.
    
regex := "((?:\p{Xwd}+\P{Xwd}){0,7})\$((?:\P{Xwd}\p{Xwd}+){0,7})"
    While pos := RegExMatch(test, regex, y, pos ? pos + 1 : 1)
    numm.Push(y)
    
    For each, n in numm
      
    one := numm[1] ; should contain: this is just a test ok testing $5 dollars for that ice-cream sandwhich is not
    two := numm[2] ; should contain: all. And also another sentence to test $10 thousand for an ice-cream sand which ain't
    
    MsgBox % one 
    MsgBox % two 
    
    return

User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: Extract the 7 words before every '$' that appears and the 7 words after it (or until period)  Topic is solved

Post by mikeyww » 26 Feb 2023, 21:51

We should assume that you have not actually run the posted script even once, right?

This may need adjustments.

Code: Select all

#Requires AutoHotkey v1.1.33
dollar := [], dot := []
str    := "so this is just a test ok testing $5 dollars for that ice-cream sandwhich is not bad at all. "
        . "And also another sentence to test $10 thousand for an ice-cream sandwich ain't bad either."
For wordNum, word in wordList := StrSplit(str, " ")
   (SubStr(word, 1, 1) = "$") && dollar.Push(wordNum)
 , (InStr(word, "."))         && dot.Push(wordNum)
For posNum, pos in dollar {
 txt   := ""
 start := Max(pos - 7, (prev := dot[posNum - 1]) ? prev + 1 : 0)
 end   := Min(pos + 7, dot[posNum] ? dot[posNum] : pos + 7)
 Loop 15
  txt .= wordList[n := A_Index + start - 1] " "
 Until (n = end)
 MsgBox 64, String, % Trim(txt)
}

samt
Posts: 62
Joined: 09 Sep 2022, 13:29

Re: Extract the 7 words before every '$' that appears and the 7 words after it (or until period)

Post by samt » 27 Feb 2023, 23:51

mikeyww wrote:
26 Feb 2023, 21:51
We should assume that you have not actually run the posted script even once, right?

This may need adjustments.

Code: Select all

#Requires AutoHotkey v1.1.33
dollar := [], dot := []
str    := "so this is just a test ok testing $5 dollars for that ice-cream sandwhich is not bad at all. "
        . "And also another sentence to test $10 thousand for an ice-cream sandwich ain't bad either."
For wordNum, word in wordList := StrSplit(str, " ")
   (SubStr(word, 1, 1) = "$") && dollar.Push(wordNum)
 , (InStr(word, "."))         && dot.Push(wordNum)
For posNum, pos in dollar {
 txt   := ""
 start := Max(pos - 7, (prev := dot[posNum - 1]) ? prev + 1 : 0)
 end   := Min(pos + 7, dot[posNum] ? dot[posNum] : pos + 7)
 Loop 15
  txt .= wordList[n := A_Index + start - 1] " "
 Until (n = end)
 MsgBox 64, String, % Trim(txt)
}
Thanks very much, that works really well.
I have code (below) that can cut off any words that come before a period and then space ". ", so that way there's no period before the '$' and the irrelevant words before it as I don't need two partial sentences in a string. That part is done.

Code: Select all

2::
extractedString :="is just. A test ok 5.9% testing $5.5 dollars for that ice-cream sandwhich is not"
PosPeriod := InStr(extractedString, ".")
PosSpace := InStr(extractedString, ". ")

if (PosSpace) ; to cut out anything before ". "
final := SubStr(extractedString, PosPeriod+1)  
else  if (!PosSpace) ; doesn't change extracted string, leaves it alone
final := extractedString
MsgBox, % final
return
output: A test ok 5.9% testing $5.5 dollars for that ice-cream sandwhich is not

I originally did not think I needed to do this, but, using your code, I've been trying to assign a separate variable for each pair of extracted arrays/string, or 1 variable per array (1 for dollar[1], 1 for dot[1]) and then concatenating them, all to show each full extraction for each '$' per variable, but no success.

Code: Select all

dollarOne := dollar[1]
dotOne := dot[1]
one := msgbox %dollarOne%%dotOne% ; just combine them simply
Tried different things, different variables in your code, and different code snippets but the outputs keeps being position numbers or blanks so I'm clearly waay off. Very lost. The array indexes of dollar and dot don't output the string that I assumed they would be holding.

So with a string like this:

Code: Select all

str := "Yes So this is just. A test testing $5 dollars for that ice-cream sandwhich is not bad at all. " . 
"And also another sentence to test $10 thousand for an ice-cream sandwich ain't bad either. so this is just" . 
" a test ok testing $5 dollars for that ice-cream( sandwhich is not bad at all."
each full extracted string (7 words before and after every '$') should be contained in 1 variable each:
one := So this is just. A test testing $5 dollars for that ice-cream sandwhich is not
two := all. And also another sentence to test $10 thousand for an ice-cream sandwich ain't bad

Also, I am not sure what is meant by "We should assume that you have not actually run the posted script even once, right?"

User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: Extract the 7 words before every '$' that appears and the 7 words after it (or until period)

Post by mikeyww » 28 Feb 2023, 06:29

Based on your post, I'm a bit confused about what you need, so I will leave the changes to you. The Max line in the script adjusts for a preceding dot (period) by excluding it (and preceding text), so you can always modify that if needed.

When I indicated that you had not run your original script, I meant that the script cannot be run due to its syntax error. I am assuming that you never saw the error message because you never ran the script that you posted. I generally think it is a bad idea to post scripts that yield error messages unless your question is about the error or the message. Otherwise, you are wasting the reader's time as well as your own.

samt
Posts: 62
Joined: 09 Sep 2022, 13:29

Re: Extract the 7 words before every '$' that appears and the 7 words after it (or until period)

Post by samt » 28 Feb 2023, 11:01

mikeyww wrote:
28 Feb 2023, 06:29
Based on your post, I'm a bit confused about what you need, so I will leave the changes to you. The Max line in the script adjusts for a preceding dot (period) by excluding it (and preceding text), so you can always modify that if needed.

When I indicated that you had not run your original script, I meant that the script cannot be run due to its syntax error. I am assuming that you never saw the error message because you never ran the script that you posted. I generally think it is a bad idea to post scripts that yield error messages unless your question is about the error or the message. Otherwise, you are wasting the reader's time as well as your own.
I see, my apologies. I tend to over explain things...need to work on that. I guess I also included some stuff that wasn't necessary. And sorry about my script that did not work. I'm guessing I typed out the string for the 'test' variable on here and forgot the quotes, but good to know about the dumb error I made. Amazingly I saw no error this whole time and didn't think to test out what I wrote for that!

What I haven't been able to figure out is, from your script that you kindly posted below that extracts 7 words before and after every '$' into 1 string each, how would I assign each extracted string (of basically what shows up in each msgbox) into 1 variable each?

Code: Select all

#Requires AutoHotkey v1.1.33
dollar := [], dot := []
str    := "so this is just a test ok testing $5 dollars for that ice-cream sandwhich is not bad at all. "
        . "And also another sentence to test $10 thousand for an ice-cream sandwich ain't bad either."
For wordNum, word in wordList := StrSplit(str, " ")
   (SubStr(word, 1, 1) = "$") && dollar.Push(wordNum)
 , (InStr(word, "."))         && dot.Push(wordNum)
For posNum, pos in dollar {
 txt   := ""
 start := Max(pos - 7, (prev := dot[posNum - 1]) ? prev + 1 : 0)
 end   := Min(pos + 7, dot[posNum] ? dot[posNum] : pos + 7)
 Loop 15
  txt .= wordList[n := A_Index + start - 1] " "
 Until (n = end)
 MsgBox 64, String, % Trim(txt)
}

User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: Extract the 7 words before every '$' that appears and the 7 words after it (or until period)

Post by mikeyww » 28 Feb 2023, 11:39

Code: Select all

#Requires AutoHotkey v1.1.33
dollar := [], dot := [], var := []
str    := "so this is just a test ok testing $5 dollars for that ice-cream sandwhich is not bad at all. "
        . "And also another sentence to test $10 thousand for an ice-cream sandwich ain't bad either."
For wordNum, word in wordList := StrSplit(str, " ")
   (SubStr(word, 1, 1) = "$") && dollar.Push(wordNum)
 , (InStr(word, "."))         && dot.Push(wordNum)
For posNum, pos in dollar {
 txt   := ""
 start := Max(pos - 7, (prev := dot[posNum - 1]) ? prev + 1 : 0)
 end   := Min(pos + 7, dot[posNum] ? dot[posNum] : pos + 7)
 Loop 15
  txt .= wordList[n := A_Index + start - 1] " "
 Until (n = end)
 MsgBox 64, String, % show := Trim(txt)
 var.Push(show)
}
MsgBox % var[1]
MsgBox % var[2]


Post Reply

Return to “Ask for Help (v1)”