I want to try and regex this text to only get "Second Baptist School" as the output by using Customer: as the set beginning for it to recognize. How would I get it so that it recognizes the beginning and gets all of the text in between the large sections of blanks?
Customer: Second Baptist School Date of Sale: 9/26/2022
Right now I'm using Customer:\s*([^ -.]+) but it only gets "Second" as the output.
You can look for 2 or more white spaces with:
Customer:\s*(.*?)\s{2,}
this should align with your above examples. The {2,} says 2 or more.
https://regex101.com/r/1HapOO/1
I'm new to Applescript and the world of coding in general. I'm trying small scripts to get my feet wet and have some fun.
I want to make a random prompt generator. I know how to set the variable to a list but what I have in mind is much bigger than a few choices. I was wondering how to get the information from a text file. In said file I have over 200 prompts to choose from. I want to make a script that selects a random one from among these and displays them in a dialog box. I'm having trouble setting the initial variable to the contents of the file as a list. The follow up (random selection) I think I have a pretty good grasp on. I hope this is clear and thank you for looking.
Edit:
Here's my code so far. I keep running into an error: "Can't make "/Users/Home/Desktop/text.rtf" into type file." Not sure what that means.
set draw_promptList to {}
set draw_prompt to read "/Users/Home/Desktop/text.rtf"
repeat with i from 1 to count of paragraphs in draw_prompt
set end of draw_prompt to paragraph i of draw_prompt
end repeat
set the list_count to the count of draw_prompt
set pick to random number from 1 to list_count
display dialog "Try drawing " & some item of draw_prompt & return
Edit 9/28:I fixed it per the revisions and it works. It does exactly what I wanted: selects a random word from among the long list of words in the text file.
set draw_promptList to {}
set draw_prompt to read "/Users/Home/Desktop/test.txt"
set the list_count to the count of paragraphs in draw_prompt
set pick to random number from 1 to list_count
repeat with i from 1 to count of paragraphs in draw_prompt
set end of draw_promptList to paragraph i of draw_prompt
end repeat
display dialog "Try drawing " & some item of draw_promptList
If I understand correctly what you're asking, reading a file and turn each line of that file into an item in a list, then here is one way it can be done.
set theFileOfPromptsList to {}
set theFileOfPrompts to read "/path/to/FileOfPrompts"
repeat with i from 1 to count of paragraphs in theFileOfPrompts
set end of theFileOfPromptsList to paragraph i of theFileOfPrompts
end repeat
If you want to insure you don't add blank lines, if there are any, to the list, then use the following instead:
set theFileOfPromptsList to {}
set theFileOfPrompts to read "/path/to/FileOfPrompts"
repeat with i from 1 to count of paragraph in theFileOfPrompts
if paragraph i of theFileOfPrompts is not "" then
set end of theFileOfPromptsList to paragraph i of theFileOfPrompts
end if
end repeat
I have a very large text file that I have opened it in VIM and I need to add 1 to numbers matching some criteria. for example:
Comment 1
Comment 2
[p2,0,0,115]Live! ConcertGrand
[p2,2,104,5]Live! PopGrand
[p2,3,104,4]Live! RockPiano
[p2,4,104,3]Live! AmbientPiano
End of file.
and I'd like to transform this (by adding say 1 to the second element of the list) to
Comment 1
Comment 2
[p2,1,0,115]Live! ConcertGrand
[p2,3,104,5]Live! PopGrand
[p2,4,104,4]Live! RockPiano
[p2,5,104,3]Live! AmbientPiano
End of file.
How can I do this in vim, please?
I have tried:
%s/\[p\zs\d\+\(,\d\+\)\=/\=(1+str2nr(submatch(1)))/
But it does not work. Any help would be greatly appreciated. CS
Do you really need to do this with search and replace? Because there is builtin functionality for addition and subtraction.
:h CTRL-A
CTRL-A: Add [count] to the number or alphabetic character at or after the cursor.
{Visual}CTRL-A: Add [count] to the number or alphabetic character in the highlighted text. {not in Vi}
So you basically could use VISUAL-BLOCK selection on your column of numbers and press CTRL-A and it will add 1 to all of them
If it is more complicated you could use macro
I have a text with the word 'article' repeated, I need using Notepad ++ to replace each of that word with a number, starting with '1', then the next instance '2'.. etc. something like {\d+[+1]}
For example:
this is article and this is another article. Here is an article etc.
becomes:
this is 1 and this is another 2. Here is an 3 etc.
Yes you can, but this is tricky.
Assuming you don't have any # in your file, replace each line by the next one (replace all and multiple times):
(?<!#)(#*article)([^#]*?)((?=\1))
\1\2#\3
So this gives: this is article and this is another #article. Here is an ##article etc.. Continue with:
article
\#
So you have:
this is # and this is another ##. Here is an ### etc.
Now every article is mapped to n times the number of pounds of its position. To convert to base 10, you need to perform the following operations alternatively and repeatingly (assuming that % does not appear in the original text)
#{10}
%
%{10}
#
And so on until you cannot replace neither the first nor the second. Then you can do:
(#|%)\1{8}
9
(#|%)\1{7}
8
(#|%)\1{6}
7
(#|%)\1{5}
6
(#|%)\1{4}
5
(#|%)\1{3}
4
(#|%)\1{2}
3
(#|%)\1{1}
2
(#|%)
1
And voilĂ ! this is 1 and this is another 2. Here is an 3 etc. If you have n articles in your document, The first operation takes O(n) times to click on "replace all", the second O(log(n)), the third O(1), so the total time is actually O(n) which is what you would have expected.
I have a file (ratings.lst) downloaded from IMDB Interfaces. The content appears to be in in the following format :-
Distribution Votes Rating Title
0000001222 297339 8.4 Reservoir Dogs (1992)
0000001223 64504 8.4 The Third Man (1949)
0000000115 48173 8.4 Jodaeiye Nader az Simin (2011)
0000001232 324564 8.4 The Prestige (2006)
0000001222 301527 8.4 The Green Mile (1999)
My aim is to convert this file into a CSV file (comma separated) with the following desired result (example for 1 line) :
Distribution Votes Rating Title
0000001222, 301527, 8.4, The Green Mile (1999)
I am using textpad and it supports regex based search and replace. I'm not sure what type of regex is needed to achieve the above desired results. Can somebody please help me on this. Thanks in advance.
The other regular expressions are somewhat overcomplicated. Because whitespace is guaranteed not to appear in the first three columns, you don't have to do a fancy match - "three columns of anything separated by whitepace" will do.
Try replacing ^(.+?)\s+(.+?)\s+(.+?)\s+(.+?)$ with \1,\2,\3,"\4" giving the following output (using Notepad++)
Distribution,Votes,Rating,"Title"
0000001222,297339,8.4,"Reservoir Dogs (1992)"
0000001223,64504,8.4,"The Third Man (1949)"
0000000115,48173,8.4,"Jodaeiye Nader az Simin (2011)"
0000001232,324564,8.4,"The Prestige (2006)"
0000001222,301527,8.4,"The Green Mile (1999)"
Note the use of a non-greedy quantifier, .+?, to prevent accidentally matching more than we should. Also note that I've enclosed the fourth column with quote marks "" in case a comma appears in the movie title - otherwise the software you use to read the file would interpret Avatar, the Last Airbender as two columns.
The nice tabular alignment is gone - but if you open the file in Excel it will look fine.
Alternately, just do the entire thing in Excel.
First replace all " with "" then do this:
Find: ^\([0-9]+\)[ \t]+\([0-9]+\)[ \t]+\([^ \t]+\)[ \t]+\(.*\)
Replace with: \1,\2,\3,"\4"
Press F8 to open Replace dialog
Make sure Regular Expression is selected
In Find what: put: ^([[:digit:]]{10})[[:space:]]+([[:digit:]]+)[[:space:]]+([[:digit:]]- {1,2}\.[[:digit:]])[[:space:]]+(.*)$
In Replace with: put \1,\2,\3,"\4"
Click Replace All
Note: This uses 1 or more spaces between fields from ratings.lst - you might be better off specifying the exact number of spaces if you know it.
Also Note: I didn't put spaces between the comma seperated items, as generally you don't, but feel free to add those in
Final Note: I put the movie title in quotes, so that if it contains a comma it doesn't break the CSV format. You may want to handle this differently.
MY BAD This is a C# program. I will leave it up for an alternate solution.
The ignorepattern whitespace is for commenting the pattern.
This will create data which can be placed into a CSV file. Note CSV files do not have optional whitepsace in them as per your example....
string data =#"Distribution Votes Rating Title
0000001222 297339 8.4 Reservoir Dogs (1992)
0000001223 64504 8.4 The Third Man (1949)
0000000115 48173 8.4 Jodaeiye Nader az Simin (2011)
0000001232 324564 8.4 The Prestige (2006)
0000001222 301527 8.4 The Green Mile (1999)
";
string pattern = #"
^ # Always start at the Beginning of line
( # Grouping
(?<Value>[^\s]+) # Place all text into Value named capture
(?:\s+) # Match but don't capture 1 to many spaces
){3} # 3 groups of data
(?<Value>[^\n\r]+) # Append final to value named capture group of the match
";
var result = Regex.Matches(data, pattern, RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace)
.OfType<Match>()
.Select (mt => string.Join(",", mt.Groups["Value"].Captures
.OfType<Capture>()
.Select (c => c.Value))
);
Console.WriteLine (result);
/* output
Distribution,Votes,Rating,Title
0000001222,297339,8.4,Reservoir Dogs (1992)
0000001223,64504,8.4,The Third Man (1949)
0000000115,48173,8.4,Jodaeiye Nader az Simin (2011)
0000001232,324564,8.4,The Prestige (2006)
0000001222,301527,8.4,The Green Mile (1999)
*/