Replacing numbers in a string with Applescript and BBEdit - replace

I am a total beginner and have written a few little scripts that feed use Applescript to feed BBEdit a list of find and replace fields, primarily for formatting. I.e. convert to tag1, tag2, etc.
I'm trying to use the same method to replace some numbers so find 9 replace with 1. It only works if the number has a space either side. I.e. tag9 9, is replaced with tag9 1. I wondered if someone could tell me why?
Here is my script:
set line1replaceList to {{"0", "1"}, {"9", "1"}, {"8", "1"}, {"7", "1"}, {"6", "1"}, {"5", "1"}, {"4", "1"}, {"3", "1"}, {"2", "1"}}
tell application "BBEdit"
tell window 1
repeat with thePair in line1replaceList
replace (item 1 of thePair) using (item 2 of thePair) options {starting at top:true, case sensitive:false, match words:true, search mode:grep}
# Check the "Search Options" in TextWrangler's scipting dictionary!
end repeat
end tell
end tell
Thanks in advance for your help.
Tom

Get rid of the "match words" part and it should work. In fact, you don't need the grep part, either.
So the line would be:
replace (item 1 of thePair) using (item 2 of thePair) options {starting at top:true, case sensitive:false}

Related

how to remove some strings in emeditor(Regex )

i want to remove some string and save other part of string that i need from a file with emeditor ..
file line like :
{"message":"{\"_\":\"user\",\"pFlags\":{\"contact\":true},\"user_flags\":2143,\"id\":702212125,\"access_hash\":\"914250561826\",\"first_name\":\"david\",\"last_name\":\"jones\",\"username\":\"david_d192\",\"phone\":\"051863329875\",\"status\":{\"_\":\"userStatusRecently\"}}","phone":"051863329875","version":"3","type":"unknown","token":"1556189892619764206","p_id":702212125,"username":"david_d192","type":"redis","user_flags":2143,"host":"win",from":"contacts"}
{"index": {"_type": "_doc", "_id": "36GG54F"}}
{"message":"{\"_\":\"user\",\"pFlags\":{\"contact\":true},\"user_flags\":2143,\"id\":702212125,\"access_hash\":\"914250561826\",\"first_name\":\"david\",\"last_name\":\"jones\",\"username\":\"david_d192\",\"phone\":\"051863329875\",\"status\":{\"_\":\"userStatusRecently\"}}","phone":"051863329875","version":"3","type":"unknown","token":"1556189892619764206","p_id":702212125,"username":"david_d192","type":"redis","user_flags":2143,"host":"win",from":"contacts"}
{"index": {"_type": "_doc", "_id": "36GG54F"}}
{"message":"{\"_\":\"user\",\"pFlags\":{\"contact\":true},\"user_flags\":2143,\"id\":702212125,\"access_hash\":\"914250561826\",\"first_name\":\"david\",\"last_name\":\"jones\",\"phone\":\"051863329875\",\"status\":{\"_\":\"userStatusRecently\"}}","phone":"051863329875","version":"3","type":"unknown","token":"1556189892619764206","p_id":702212125,"type":"redis","user_flags":2143,"host":"win",from":"contacts"}
{"index": {"_type": "_doc", "_id": "36GG54F"}}
i want to save id, first_name , last_name , phone , username(if exist) in every line =>
id:702212125 first_name:david last_name:jones phone:051863329875 username:david_d192,
id:702212125 first_name:david last_name:jones phone:051863329875 username:david_d192,
id:702212125 first_name:david last_name:jones phone:051863329875,
how i can do this ?
thanks
JSON parsing is the optimal way to do this (https://linuxconfig.org/how-to-parse-data-from-json-into-python). But you can make life harder and use regex (here presented in PCRE (PHP) flavor):
Get all id's:
(?<=id\":\s\")(\w+)(?=\")
See example:
https://regex101.com/r/g5vfEd/1
Get all first names:
(?<=first_name\\\":\\\")(\w)+(?=\\)
See example:
https://regex101.com/r/g5vfEd/2
Get all last names:
(?<=last_name\\\":\\\")(\w)+(?=\\)
See example:
https://regex101.com/r/g5vfEd/3
Get all phone numbers:
(?<=phone\\\":\\\")(\w)+(?=\\)
See example:
https://regex101.com/r/g5vfEd/4
Get all user names if they exist:
(?<=username\\\":\\\")(\w)+(?=\\)
See example:
https://regex101.com/r/g5vfEd/5
complete pattern to match everything:
id\\?\":\s?\"?(\w+),?[\\\"].*first_name\\\":\\"(\w+).*last_name\\\":\\\"(\w+).*phone\":\"(\d+).*(?=username)?\":\"(\w+).*
Returns 3 matches, each with the following 5 groups (here match 1 is shown):
Group 1. 85-94 702212125
Group 2. 145-150 david
Group 3. 169-174 jones
Group 4. 285-297 051863329875
Group 5. 454-462 contacts
See link: https://regex101.com/r/g5vfEd/6
As you've tagged regex and Emeditor you can try this.
Emeditor version 19.1 onwards supports regex named groups like this:
(?<id>expression)
and named backreference by using this form:
\k<id>
So steps:
Find and Replace (Ctrl-H). Tick "Match Case" and select "Regular Expressions".
Find:
\\"id\\"[\\":]*(?<id>[^\\":,]*).*?\\"first_name\\"[\\":]*(?<first_name>[^\\":,]*).*?\\"last_name\\"[\\":]*(?<last_name>[^\\":,]*).*?\\"phone\\"[\\":]*(?<phone>[^\\":,]*)(.*?"username"[\\":]*(?<username>[^\\":,]*))?
Replace with:
id:\k<id>\tfirst_name:\k<first_name>\tlast_name:\k<last_name>\tphone:\k<phone>\tusername:\k<username>
Click the down Arrow next to the Extract button and select "To New Document"
Click the Extract button to output to a new tab delimited file.

Find number and replace + 1

I have a large file with a list of objects that have an incrementing page # ie
[
{page: 1},
{page: 2},
{page: 3}
]
I can find each instance of page: # with page: (\d) in vscode's ctrl+f finder. How would I replace each of these numbers with # + 1?
It can be done rather easily in vscode using one of emmet's built-in commands:
Emmet: Increment by 1
Use your regex to find all the page: \d+ in your file.
Ctrl-Shift-L to select all those occurrences.
Trigger the Emmet: Increment by 1 command.
Here is a demo:
It's not possible to perform arithmetic with regex. I use LINQPad to execute these small kind of scripts. An example of how I would do it is in the c# program below.
void Main()
{
var basePath = #"C:\";
// Get all files with extension .cs in the directory and all its subdirectories.
foreach (var filePath in Directory.GetFiles(basePath, "*.cs", SearchOption.AllDirectories))
{
// Read the content of the file.
var fileContent = File.ReadAllText(filePath);
// Replace the content by using a named capture group.
// The named capture group allows one to work with only a part of the regex match.
var replacedContent = Regex.Replace(fileContent, #"page: (?<number>[0-9]+)", match => $"page: {int.Parse(match.Groups["number"].Value) + 1}");
// Write the replaced content back to the file.
File.WriteAllText(filePath, replacedContent);
}
}
I also took the liberty of changing your regex to the one below.
page: (?<number>[0-9]+)
page: matches with "page: " literally.
(?<number> is the start of a named capture group called number. We can then use this group during replacement.
[0-9]+ matches a number between 0 and 9 one to infinite times. This is more specific than using \d as \d also matches other number characters.
The + makes it match more than on digit allowing for the number 10 and onwards.
) is the end of a named capture group.
You could do that in Ruby as follows.
FileIn = "in"
FileOut = "out"
File let's construct a sample file (containing 37 characters).
File.write FileIn, "[\n{page: 1},\n{page: 2},\n{page: 33}\n]\n"
#=> 37
We may now read the input file FileIn, convert it and write it to a new file FileOut.
File.write(FileOut, File.read(FileIn).
gsub(/\{page: (\d+)\}/) { "{page: #{$1.next}}" })
Let's look at what's be written.
puts File.read(FileOut)
[
{page: 2},
{page: 3},
{page: 34}
]
I've gulped the entire file, made the changes in memory and spit out the modified file. If the original file were large this could be easily modified to read from and write to the files line-by-line.
Adding another answer as it is significantly different than the other. I wrote an extension Find and Transform which makes it easy to do math in a find in a file.
In this case with this keybinding (in your keybindings.json file):
{
"key": "alt+r", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "page: (\\d)",
"replace": "page: $${ return $1 + 1 }$$",
"isRegex": true
}
[That could also be a setting in your settings.json file if you wish with slightly different syntax of course.]
The $${ return $1 + 1 }$$ represents a javascript operation. Here 1 will be added to capture group 1 from the find regex.
Within the $${ ... }$$ almost any javascript operation can be inserted. There are many examples in the repo.

find string containing special characters in mongodb using $regex

using $regex in mongodb, I want to find the name B&B Hôtel which contain some special characters like & and ô by typing BB Hotel.
I tried this code:
db.txt.find({ "name": {'$regex': query, $options:'i'}})
where query can be BB Hotel.
You don't want regex search, you want diacritic insensitive text search
"name":{
$text:
{
$search: "\"B&B Hotel\""
$caseSensitive: false,
$diacriticSensitive: false
}
}
Note that $diacriticSensitive defaults to false, but I never trust the defaults. If you are running with older indexes (version 2 or less text index), you may not be able to use the indexes. The escaped " in the search part is to search for this phrase.

Regex to extract names from a string into a list

How is it possible to use regex just to extract the names from the following string:
Liam got 6,andy got 6
And add it to a list, i've tried using regex but i cant find the correct expression to extract just the names and am still a bit shaky on this area.
any help would be appreciated
For simple case, I always recommend not to use Regex, you could do it like this using string.Split, string.Replace, and LINQ Where:
Dim names As String() = sentence.Replace("got ", "").Split(" ").Where(Function(t) Char.IsLetter(t(0))).ToArray()
Suppose you have this sentence:
Dim separators As Char() = {",", " "}
Dim names As String() = sentence.Replace("got ", "").Split(separators, System.StringSplitOptions.RemoveEmptyEntries).Where(Function(t) Char.IsLetter(t(0))).ToArray()
What happen step by step is:
"Andy got 6,may got 10, blue got 9, hERald got 0"
"Andy 6,may 10, blue 9, hERald 0" 'After replace
"Andy" "6" "may" "10" "blue" "9" "hERald" "0" 'After split
"Andy" "may" "blue" "hERald" 'After where
This should work in vb.net.
(?<=^|,)\w+
https://regex101.com/r/wT8rE9/1
And if it can have a space after the comma:
(?<=^|,|,\s)\w+
If you're comfortable with capture groups, you could do the following which should be more efficient:
(?:^|,\s*)(\w+)

Regex get between 2 string with spaces

how could I parse this response text using Regex?
info = {
"title": "Developers",
"image": "http://i.ytimg.com/vi/KMU0tzLwhbE/default.jpg",
"length": "3",
"status": "serving",
"progress_speed": "",
"progress": "",
"ads": "",
"pf": "http://70efd.pf.aclst.com/ping.php/10754233/KMU0tzLwhbE?h=882634",
"h": "87d0670f6822946338a610a6b9ec5322",
"px": ""
};
The outcome I need should look like this "87d0670f6822946338a610a6b9ec5322", however, I can't get the correct syntax. I'm new to using Regex and what I have tried using is "\s+", can anyone point me in the right direction?
If you must use a regex, you could use a regex along the lines of:
"h" : "(.+?)",
You can see an example of it here. Just read from the first capture group and that would select your text.
That looks like like JSON aside from the info = prefix. If you have any specific language you are working in that could parse JSON, that might be a better way of handling that input.
You could also use (?<="h": ")[a-z0-9]+(?="), which will match any sequence of lowercase letters and numbers, as long as the sequence is preceded by "h": " and followed by ". I made an explanation and demonstration here.