How to replace "&A-Z" with the orinal text minus the & - regex

I'm looking for a way to replace "&" from a text string when it's followed by text. For example;
Input Text: "Ne&w && Edit Record"
Required Output: New & Edit Record"
The reason for "Ne&w" is that "&" then shows w as the shortcut key (underscored in the UI) and the reason for the "&&" is so that a single "&" is displayed in the text.
When using the input text as the value for the text propery on a command button the text displays as expected, but when I pass the text property to a message box it displays the input text in the message box dialog.
I don't want to use the Tag property to store the message I want in the message box as I use the tag property for another value.

Try this:
Dim input As String = "Ne&w && Edit Record"
Dim output As String = "New & Edit Record"
Dim p As String = Regex.Replace(input, "&(?<first>\w|&)", "${first}")
MessageBox.Show(output = p) 'shows True
In the above Regex expression I am capturing an ampersand followed by either a letter or another ampersand, and replacing that sequence with a symbol coming after the ampersand. <first> is a named group, it is used for Regex replacement.
See Regex.Replace on MSDN.

You can remove all & signs not followed by a & sign. Match them like this:
&(?!&)
See it in action

Related

Split mixed upper and lowercase in the same word into two lines

I have a string in cells that is lacking new lines.
It looks like this:
Text Text TextText Text Text T5df Tdfcv TextNeu
In other words:
If there is a change from Lowercase to Uppercase within a word, this is where a new line should be inserted as \n.
So the example would convert to
Text Text Text
Text Text Text T5df Tdfcv Text
Neu
Resp.:
Text Text Text\nText Text Text T5df Tdfcv Text\nNeu
I found
String[] r = s.split("(?=\\p{Lu})");
I tried REGAUS(F2;"(?=\\p{Upper})";"\n";"g") yet I get a 502, as something is wrong with the regex.
Which formula do I need for calc to do this?
With english formula names, the following formula will do the trick:
=REGEX(A1;"([:lower:])([:upper:])";"$1"&CHAR(10)&"$2";"g")
Same on multiple lines for sake of readability:
=REGEX(
A1;
"([:lower:])([:upper:])";
"$1" & CHAR(10) & "$2";
"g"
)
It matches a lower-case letter followed by an upper-case letter, and inserts a newline using the CHAR() function.
You'll have to adapt the line heigth manually, otherwise you will see only "Neu" (the last line).
For localised formula names (german), it would be:
=REGAUS(A1;"([:lower:])([:upper:])";"$1"&ZEICHEN(10)&"$2";"g")
I would have expected that inserting "\n" should work, too, but i did'nt manage got get it working, thus the recourse to CHAR(10).

Remove numbers and parentheses at the beginning of each line in Notepad++

Basically every line in my text file is formatted like this:
1) Baker
2) Photographer
3) Doctor
4) Teacher
5) CEO
etc, etc.
Using Notepad++ how do I remove the 1), 2), 3), etc?
1 ) Press Ctrl + H for pop window to Replace.
2 ) Provide the ^\d+\) regular expression in Find what text field, choose option Regular expression in search mode and make empty in Replace with text field.
3 ) Click on Replace all or Replace button.
You can use the regex ^\d+\) (note the trailing whitespace) and replace text matching this pattern with an empty string.
Step-by-step guide:
Press Ctrl+h to open the Replace dialog.
Press Alt+F to focus on the Find text field.
Enter ^\d+\) and press Alt+l to switch to the Replace with text field.
Press backspace to delete any text that might be in this box.
Press Alt+g to switch the search mode to regular expression.
Press Alt+a to perform a replace all operation on the document.
You can use the column selection mode, normally accessed with alt + mouse, or by the edit menu, select the 2 columns, and delete them as any text

Manage looping on txt file with AppleScript

I have a text file that looks like this: screenshot below
http://i.stack.imgur.com/AqKzS.png
Each item has this format:
ID<>Text
~~
ID<>Text
~~
I want to fetch the ID in an INT to be used later. And the Text in a String to be used later.
I looped over the file many times using delimiters "<>" & "~~". However, I fail each time with a different script error.
first I faced difficulties because the file contains a lot of newlines throughout the "Text". Also, the text sometimes contains an English paragraph followed by an Arabic paragraph, as showed in the Screenshot.
The ID as highlighted should be {9031} and the Text should be {N/M06"El Patio.......
......
....
....
....
Arabic Text.....}
Can someone help me with the correct script to loop over this text file and fetch each ID followed by its text to be used in a DataEntry process?
For this purpose I recommend to install Satimage sax 3.7.0
The benefit is to find text with regular expression.
Then you easily filter the text with find text
set theText to read file "HD:Path:to:text.txt" as «class utf8» -- replace the HFS path with the actual path
set theResult to {}
set matches to find text "\\d{1,4}<>.*" in theText with regexp and all occurrences
repeat with aMatch in matches
tell aMatch's matchResult
set end of theResult to {text 1 thru 4, text 7 thru -1}
end tell
end repeat
find text returns a record:
matchLen: length of the match
matchPos: offset of the match (0 is the first character!)
matchResult: the matching string (possibly formatted according to the "using" parameter)
The result of the script in variable theResult is a list of lists containing the id and the text. The text starts after the <> but you might cut more characters.
Edit:
It seems that the regex can't parse this text (or my regex knowledge is too bad).
This is a pure AppleScript version without the Scripting Addition.
set theText to read file ((path to desktop as text) & "description.txt") as «class utf8» -- replace the HFS path with the actual path
set {TID, text item delimiters} to {text item delimiters, ("~~" & linefeed)}
set theMatches to text items of theText
set text item delimiters to TID
set theResult to {}
repeat with aMatch in theMatches
if length of aMatch > 1 then
tell aMatch
set end of theResult to {text 1 thru 4, text 7 thru -1}
end tell
end if
end repeat

Using VBScript to find and replace all multi-line text between curly braces of a node within a JSON file

As part of a Windows logon script (hence the VBScript requirement), I would like to set values in a user's Google Chrome preferences (stored in a JSON file in the user profile) to apply download settings when they logon.
I'm trying to achieve the following:
Open a JSON file (%userprofile%\Local Settings\Application Data\Google\Chrome\User Data\Default\Preferences) and read the contents to a string;
Search for a particular node named "download", which by is pre-populated with multi-line values that may vary between builds;
Replace the entire text between the braces with specified multi-line text; and
Write the updated string to the original file and save.
The full JSON file is fairly large, but as a sample to use as the input, this is copied from a typical Google Chrome prefs JSON file:
"bookmark_bar": {
"show_on_all_tabs": false
},
"download": {
"directory_upgrade": true,
"prompt_for_download": false
},
"sync": {
"suppress_start": true
},
I would like to programmatically search for the "download" node, and replace everything between the braces of just this node so that it reads:
"download": {
"default_directory": "C:\\Windows\\Temp",
"extensions_to_open": "pdf",
"prompt_for_download": false
},
...with the rest of the file's contents unchanged.
Given the whitespace and multiple lines in the section of the JSON to be replaced, as well as the wildcard requirement to include all/any text between the braces, I can't do this using the VBScript Replace function, but my RegEx knowledge is limited.
You can do the replacement with a regular expression:
prefsFile = "%userprofile%\Local Settings\...\Preferences"
prefsFile = CreateObject("WScript.Shell").ExpandEnvironmentStrings(prefsFile)
newPrefs = "..."
Set fso = CreateObject("Scripting.FileSystemObject")
json = fso.OpenTextFile(prefsFile).ReadAll
Set re = New RegExp
re.Pattern = """download"": {[\s\S]*?},"
json = re.Replace(json, """download"": {" & vbCrLf & newPrefs & vbCrLf & "},")
fso.OpenTextFile(prefsFile, 2).Write(json)
The pattern [\s\S] matches any whitespace or non-whitespace character. You can't use . in this case, because that special character does not match newlines, and you want the expression to span multiple lines. The qualifiers * and ? mean "match any number of characters" and "use the shortest match" respectively. That way the expression matches everything between a pair of curly braces after the "download": keyword.

regular expressions and vba

Does anyone know how to extract matches as strings from a RegExp.Execute() function?
Let me show you what I've gotten to so far:
Regex.Pattern = "^[^*]*[*]+"
Set myMatches = Regex.Execute(temp)
I want the object "myMatches" which is holding the matches, to be converted to a string. I know that there is only going to be one match per execution.
Does anyone know how to extract the matches from the object as Strings to be displayed lets say via a MsgBox?
Try this:
Dim sResult As String
'// Your expression code here...
sResult = myMatches.Item(0)
'// or
sResult = myMatches(0)
Msgbox("The matching text was: " & sResult)
The Execute method returns a match collection and you can use the item property to retrieve the text using an index.
As you stated you only ever have one match then the index is zero. If you have more than one match you can return the index of the match you require or loop over the entire collection.
This page has a lot of information on regex and seems to have what you want.
http://www.regular-expressions.info/vbscript.html