ver. 160719_c
NOTE: this is a backup of my own notes.
-
switch on "Regular Expressions"
-
now you can go back and use regular expressions
-
start by finding a number
and replacing with
-
you'll of course need numerals in your document for this to work
-
once you've performed the search you'll see a list of found items which you can replace individually or all at the same time, and the found items will also be highlighted in your document
Above: Initial info at top - CREDIT - is from this sketchyTech post article dated Nov 16, 2012.
Purpose: only to Remind myself of stuff. No Guarantees!! Regular Expressions can radically change your files. Great when it works as expected. Back up your stuff.
Use *?
or +?
for non-greedy capture.
Regex [ ] ( ) { }
are NOT escaped! You are lucky.
For multi-line search use [\s\S]*
for the wildcard. in Textastic at least.
Keep in mind, in iOS apps with REGEX like Textastic or Workflow:
*?
or +? are non-greedy.- Tokens
[ ] ( ) { }
are not escaped. - any escaped brackets below mean actual text brackets.
- use
[\s\S]*
for crude multi-line search. - Sometimes Carrot ^ not catch line start in WorkFlow, use unicode
[\u000A]
instead. Catch it()
for the replace to keep new line.
(if viewing this as plain text, please ignore the markdown formatting, hash, tic, and star for ex.)
In Workflow, Match text
and take the first of the match list
Find:
Replace:
get 130 chars of some text, but only enough to end in a space or the end of a string, thus not ending in the middle of a word.
MATCH TEXT
##^.{0,}(?=130| $)
Match Text returns a list of matches, you want the 1st match, being the shortened subset of text.
NOTE: Workflow, This only works with plain Text, not Rich text. Some plain Text copied to clipboard reports type Rich text although it is plain Text. There are essentially 2 types of text. Get Type of "Text" is plain & "Rich text" typically has formatting, but not always as mentionted. Types have Common word "Text" and "text" but case difference, Text vs text. To capture either, use a case insensitive Get Type check for lower case text, then put a "Get text from input", or Match Text [Tt]ext
then Get Object of Class NSString to ensure it is plain Text before the lookahead Regex, this will also work for either types or text.
By the way, not relevant here, but if a Match Text list of results is zero, that is obviously no matches.
sometimes carret ^
not work in workflow.
Find:
Replace:
From 2015:09:10 09:50:59
to 2015-09-10
Unnecessary but cool.
Replace:
I extracted text from a PDF file, the footnote numbers should be in brackets.
Find a footnote in NHL text, example:
find text [28]
FIND a footnote adjacent to a period, example text
.[28]
then (REPLACE Action) add a space between them, ex.
. [28]
Note the text period is escaped, but regex dot would not be.
Search:
Replace:
Find a comma adjacent to a letter, ex
,x
then insert a space between them, like
, x
NOTE: careful, this ALSO CATCHES NUMBERS, WATCH FOR LEGIT COMMAS IN NUMBERS 7,000
Search:
Replace: ?????
Find unbracketed footnote AT START OF LINE, ex 28
(we know it needs brackets).
In other words, find two digits at the start of a line. I manually did the replace, these are the search:
Search
or
Search:
Replace:
You just wiped out a lot of text leaving only the last part.
Example, you get a massive html file, you do not need it all.
Put an obvious word phrase like __crap__
at a location to use as a very easy start for multiline pattern to blank the rest of file.
Search:
Replace:
WARNING THAT WILL BLANK ALL TO THE END OF FILE
yes, Search for text that is a backslash n: \n
Note: below i did use the symbol \n
in replace new line feed as opposed to \r
simply because \n did make new lines. Maybe \r
would have made new lines too, I don't know. (If it's an issue i can normalize line breaks later).
FIND
REPLACE WITH
ADD MORE EXAMPLES
REGEX1
USING TEXTASTIC
SEARCH: a line start followed by 4 spaces, there are four spaces in the empty paren:
REPLACE: change that to a line start followed by TAB
Next task, How do you replace each occurrence of ( )
with equal number of tabs, in one single regex expression? -- I DO NOT KNOW.
So, Eventually, i manually searched for, up to 14 groups of 4 spaces, replacing with $1 all the way up to $14, and worked backward.
After that some crap spaces was left over before first non-space.
SEARCH
REPLACE
However...
Then the text part of the page was indented 2 tabs too much.
SEARCH
REPLACE
(oh how i wish for desktop "reformat code" functions, like gVim, etc. in this iOS app).
Stuff in head of html file needs at least 1 tab (formatting), pressed replace (in search replace popup) for each individual match until i got to body tag.
SEARCH
REPLACE
Then inside text part, another tag pushed it out excessively.
SEARCH
REPLACE
Textastic, put an empty line before a script tag where none exist.
2 SCENARIOS:
find a starting script tag that is not at the start of a line. Make it the start of a line with an extra empty line above it.
example find this text:
</ul><script type="text/javascript">
SEARCH
REPLACE
Example result text after replace action:
</ul>
<script type="text/javascript">
This is for if script tag is at start of line but there is not an empty line above it.
SEARCH
REPLACE
place a line break after a closing script tag.
look for closing script tag followed by a non-line break, example find text,
</script></div>
SEARCH
replace with 2 line breaks, so you get an empty line after the script.
REPLACE
Result example:
</script>
</div>
NOTE: if u run REGEX that places blank line before script, THEN run this RegEx, you get what you want (a blank line before and after the script block) -- as a kludge.
Since this one's search does not check for closing script tag that does have a line break, but not if the desired following blank line is present. In other words:
</script>
<script>
You would want a blank line there after the closing tag.
But apparently in textastic, we can only check at one time for the </script></div>
in one run, and the
</script>
<script>
in another seperate search, in other words...
We only want to check for how many line breaks are there and so we know how many we need to add to get the number of line breaks we want.
Apparently, you can not check for the number of line breaks being none or 1, and wanting 2 of \n
, for knowing how many to add to get it, does not seem to work in textastic.
(Notes much of the whitespace are tabs.
SEARCH
THIS SEARCH WORKED TOO
REPLACE
SAMPLE:
Start text you have to see this in plain text (or gitbub markdown tripple tic style is ok) to get the point
go https://duckduckgo.com/?q=%21%20%s Open first result (DuckDuckGo)
b https://duckduckgo.com/?q=%21%s Bang search (DuckDuckGo)
grep https://www.cueup.com/?q=%s&fq=1 Greplin
End up with:
go https://duckduckgo.com/?q=%21%20%s Open first result (DuckDuckGo)
b https://duckduckgo.com/?q=%21%s Bang search (DuckDuckGo)`
grep https://www.cueup.com/?q=%s&fq=1 Greplin
Note Editorial can do this automatically for you
From this
- ~ Rainbows End by Vernor Vinge
- Glasshouse by Charles Stross
To this
- *~ Rainbows End* by Vernor Vinge
- Glasshouse by Charles Stross
SEARCH
REPLACE
SEARCH
REPLACE
Example: ( tabs do not show always in MD examples)
` $someVar `
to
` $someVar`
Clipboard will give name which get name
will report text like a times stamp, sometimes a real extension is included, if so use that extension in the replace. Example starting text like Clipboard Apr 16, 2015, 12.12 PM
or Clipboard Apr 16, 2015, 1.08 PM
AND sometime with useful extension Clipboard Apr 16, 2015, 2.00 PM.txt
.
example find text like this:
Clipboard Apr 16, 2015, 12.12 PM
Clipboard Apr 16, 2015, 1.08 PM
**AND sometimes like **
Clipboard Apr 16, 2015, 2.00 PM.txt
SEARCH
REPLACE
Example result text after replace action:
Clipboard
Clipboard.txt
Basic match text for a URL pattern:
FYI, note you CAN use a colon : unescaped
^https?:[/]{2}
Match text for a .jpg
URL pattern:
After end of a text paragraphs, just after punctuation as place to do it.
SEARCH
REPLACE
WorkFlow, to recognize beginning of line, use Unicode instead of ^ Regex.
In practice, if you replace must capture it with ([\u000A]) and use in replace.
(Note: if you do a replace, and want to keep this line as its own line, you must capture that with ([\u000A]) and put it first thing in your replace.)
SEARCH:
REPLACE:
From an un-rounded, unformatted number then use replace text to format, can roughly format all numbers in a text box this way.
Example, first via workflow do Raw image size: 1462321
, divide by 1000, then:
example starting text:
1462.321
example ending text: why, u can format 5 unrounded numbers at one time like this
1,462
SEARCH
REPLACE
Displaying item SIZE as kilobytes in a workflow. Can take as many as 4 separate steps. Reduce it to 2 steps:
Here we use 2 steps.
1st: FORMAT raw size To 1 digit. example below.
2nd: use Replace Text:
SEARCH:
REPLACE:
2nd: better use Replace Text:
SEARCH:
REPLACE:
Example start text:
5,618,706.0
Desired finish text:
5,618k
This is variant B. Typically, item size is the only number you want divided by 1,000 prior to display, to show it in k. The two above steps replace of these four workflow steps:
- to divide by 1000
- and round to zero digits simply to chomp the decimals
- and format number
- and add a k
It is not trivial to do this perfectly with one workflow regex, given an unknown number of digits AND wanting to place the letter k simultaneously. Here, Format step takes care of all commas, and ensuring a known trailing pattern which we replace with the letter k.
Strip last 3 digits off a series of digits. Use Replace Text (the replace is blank):
SEARCH:
REPLACE:
Example start text:
5618706
Desired end text:
5618
Saves one step as it substitutes for these two workflow steps:
- divide by 1000
- and round to zero digits to chomp the decimals.
This is variant A perhaps useful regards format size of item as kilobytes in workflow using fewer steps.
Replace only the size item in existing pic data , like if you made jpg, size is smaller.
START
976k 1,887 x 677
REGEX
^([.\d,]+[k]\b)
REPLACE:
With new size
Textastic, MOVE THE CLOSING SPAN TO AFTER THE PRICE, CATCHING PRICES THAT MAY INCUDE CENTS
SEARCH
REPLACE
incomplete Example regex i tried for this
EXAMPLE 1 FROM, TO
This next price had cents
EXAMPLE 2 FROM
EXAMPLE 2 TO
add better examples