Applescript Regex Replace Usage

I am using Applescript to do regex replace for a pattern of type APP[0-9][0-9][a-z][a-z] and display it as a hyperlink.
Eg: APP23cc to APP23cc
       APP36ij to APP36ij
I am getting the body of the email as a string.
How could I do this?

I'm not exactly clear on what you're trying to do (display the hyperlink where and how? what link are you trying to link?), but I can say that applescript does not do regexp natively.  you have two choices:
download the Satimage osax so that you can do regexp
use a simpler search pardigm
a simpler search would require a repeat loop, like so:
repeat with w in words of body
          considering case
                    if w begins with "APP" then
  -- do whatever you're trying to do here
                    end if
          end considering
end repeat
you can make that IF as detailed as you need it to be to specify the things you want to work with.

Similar Messages

  • Variable in regex replace pattern

    Hi,
    I need to use a variable in a regex replace pattern - how can I do it? Specifically, I need to pass arguments to a shell script that then uses that argument in a replace pattern:
    #!/bin/bash
    #$1 now holds the argument
    sed 's/searchpattern/replace_pattern_with_variable$1/g' file1 > file2
    when I run this, the replace pattern uses $1 as a literal string "$1" and not the variable value. How can I change this?
    Thanks!
    Ingo

    Hi Ingo,
       As Vid points out, the issue is that single quotes protect strings from shell interpretation. You need to have the dollar sign, '$', visible to the shell or it won't read what follows as a variable name. Using double quotes works because the shell "reads through" those.
       However, complex regular expressions can contain lots of characters that the shell interprets. These can be quoted individually by backslashes but the use of backslashes in regular expressions is complex enough without the addition of shell interpretation. I find it easiest to keep the single quotes and only expose the part of the string that the shell needs to interpret.
       The shell doesn't have a special string concatenation character. All you have to do is to put the strings beside each other with nothing in between and the shell will concatenate them. Therefore it's possible to write your example as:
    sed 's/searchpattern/replace_pattern_with_variable'${1}'/g' file1 > file2
    That is, one closes the single quote right before the variable and then resumes it immediately afterward. The shell will put these quoted strings together with the contents of the variable just as it would with double quotes but you still enjoy the protection of single quotes around the rest of the string!
    Gary
    ~~~~
       P.S. Perl's master plan (or what passes for one) is to take
       over the world like English did. Er, as English did...
          -- Larry Wall in <[email protected]>

  • Efficient regex replace function? Help!

    Here's what I'm trying to do:
    Scan through a string, and replace any occurences of http://...... (until the next space) with an HTML link. Does anyone know what would be the most efficient regex replace function to do this?
    Thank you very much in advance.

    class StringReplaceDemo{
         public static void main (String args[]){
              String mystr = "This is hyperlink to Google http:// . From there you can search for any thing in the world";
              String google = "http://www.google.com";
              String hyperlink = "http://";
              int hyperlinklength = hyperlink.length();
              int index = mystr.indexOf("http://");
              mystr = mystr.substring(0,index)+google+mystr.substring((index+hyperlink.length()),mystr.length());
              System.out.println(mystr);
    }

  • Understanding Regex replace method call involving delegate

    Hello,
    I am trying to understand the $regex.replace static method call below (I came across this code snippet in the cookbook).
    $replacer = {
    param($match)
    $chars = $match.Groups[0].Value.ToCharArray()
    [Array]::Reverse($chars)
    $chars -join ''
    $regex = [Regex] "\w+"
    $regex.Replace("Hello World wide", $replacer)
    What I do not understand is the below overloaded definitions for replace method do not seem to match the above replace call. So how exactly is this working? The above call has 2 parameters passed where as none of the below overloads have less than
    3 parameters.
    PS C:\WINDOWS> [regex]::replace
    OverloadDefinitions
    static string Replace(string input, string pattern, string replacement)
    static string Replace(string input, string pattern, string replacement, System.Text.RegularExpressions.RegexOptions options)
    static string Replace(string input, string pattern, string replacement, System.Text.RegularExpressions.RegexOptions options, timespan matchTimeout)
    static string Replace(string input, string pattern, System.Text.RegularExpressions.MatchEvaluator evaluator)
    static string Replace(string input, string pattern, System.Text.RegularExpressions.MatchEvaluator evaluator, System.Text.RegularExpressions.RegexOptions options)
    static string Replace(string input, string pattern, System.Text.RegularExpressions.MatchEvaluator evaluator, System.Text.RegularExpressions.RegexOptions options, timespan
    matchTimeout)

    What you are looking at are the static methods ([regex]::) and their appropriate parameters which in this case have a minimum of 3 parameters to properly perform the Replace using the input, pattern and replacement
    value. If you were to use the constructor of [regex] to create a pattern like this:
    $Regex = [regex]'\w'
    You will see that the Replace method here allows for only 2 parameters because you have already satisfied the pattern when you created the Regex object.
    $Regex.Replace
    OverloadDefinitions
    string Replace(string input, string replacement)
    string Replace(string input, string replacement, int count)
    string Replace(string input, string replacement, int count, int startat)
    string Replace(string input, System.Text.RegularExpressions.MatchEvaluator evaluator)
    string Replace(string input, System.Text.RegularExpressions.MatchEvaluator evaluator, int count)
    string Replace(string input, System.Text.RegularExpressions.MatchEvaluator evaluator, int count, int startat)
    Boe Prox
    Blog |
    Twitter
    PoshWSUS |
    PoshPAIG | PoshChat |
    PoshEventUI
    PowerShell Deep Dives Book

  • Using a local variable in regex portion of replaceAll(regex, replacement)

    While this works..
    output = output.replaceAll("(HED>|AUT>)(.*)(</\\1)", "$1<![CDATA[$2]]>$3");
    I'd like the list of alternation values to be contained in a variable, for example:
    String nodeLIst = "HED>|AUT>";
    output = output.replaceAll("(nodeList)(.*)(</\\1)", "$1<![CDATA[$2]]>$3");
    The extension of this would be so I can store this stuff in a db as a list and avoid compilation on change, but please don't let this muddy the waters... :)
    Any pointers are much appreciated. Links to specific reading material, etc. I've scoured Friedl's Mastering Regular Expressions to no avail. This approach is supported by some other regex engines I've used (perl, php, ORO?) but I'm new to Java.
    TIA,
    Mark

    I've scoured Friedl's Mastering Regular Expressions to no avail.Did you look on page 209? In the book, that code sample is labelled "Building Up a Regex Through Variables in Java". That should have been a clue. ^_^
    But seriously, you're probably thinking of the interpolated strings you find in scripting languages like Perl, PHP, Ruby, etc.. But that's a feature of the language itself, not the regex engine, and Java doesn't work that way. (The $1, $2, etc., in the replacement string are processed by the Matcher class, in a very limited imitation of Perl's variable interpolation).
    However, you can fake it pretty well with String's format() method:   String regex = String.format("(%s)(.*)(</\\1)", theAlternation);
      output = output.replaceAll(regex, "$1<![CDATA[$2]]>$3"); That way, you can easily escape the dynamic part, in case it might contain regex metacharacters:   String regex = String.format("(%s)(.*)(</\\1)", Pattern.quote(theAlternation));

  • Applescript to replace a picture in a Word document

    I am trying to create an Applescript to control Microsoft Word to replace a selected image in a document with a new image from a file.  The extracted problematic code is below.
      -- prior code sets up variables and seems to be working fine.
                        set {start of content:t_start, end of content:t_end} to text object of selection
                        set float to shapes of active document whose end of content of its anchor ≥ t_start and end of content of its anchor ≤ t_end
                        if float is not {} then --replacing a floating image as opposed to an inline image
                                       repeat with pic in float
                set a to anchor of pic
                set t to top of pic
                set l to left position of pic
            end repeat  -- repeat is not logically necessary but Word seems happier
             tell active document
                set np to make new picture at a with properties {file name:tfile2, save with document:true}
                set relative vertical position of np to rvp
                                                     set top of np to t
                set left position of np to l
             end tell
             delete pic
                        else
         -- rest of code handles when no selection exists or the selected image is inline and is working fine.
    Everything is working fine - the existing picture is deleted and the new one is inserted and moved to the correct position but always on the FIRST page.  The anchor put into a contains the location of the old picture's anchor (which might have a start of content set to 2000 say) but when the new picture is created at a, the new picture is always created with an anchor with a start of content of 0 - regardless of the value in the original anchor.
    What I need is either to create the new picture with an anchor that is on the old page OR move the anchor to the old page after creation - neither of which I have managed to achieve so far.
    Any help or suggestions greatly appreciated.

    The solution to this problem is to make the new picture using this command
    set np to make new picture at a with properties {file name:tfile2, save with document:true, anchor:a}
    Silly me.  I though that making the picture at "a" would set the anchor to "a". 

  • RegEx replacement

    I'm trying to make a small parsing engine that does the following:
    1. reads an HTML document
    2. searches for special tags of the form [key]
    3. looks up the key in a hashmap or something
    4. replaces [key] with value from the hashmap
    I'm storing the HTML document line by line as strings in an ArrayList (any better way to do this?). I iterate through each line, replacing each tag with its corresponding value. The thing is, however, that I'd like to use RegEx as much as possible.
    The java.lang.String.matches(String regex) method tells me, through a boolean return value, whether the current line actually contains any tag at all , while the java.lang.String.replaceFirst(String regex, String replacement) method solves the replacement part. What the String class methods don't provide me with is a method for returning the actual substring that was found during the java.lang.String.matches(String regex) call. Is there any such method out there? Right now I'm doing this manually, using the String class' indexOf(...) and lastIndexOf(..) methods.
    In short, what I'm looking for is a method something like String find(String text, String regex) that would return "salary" when called as find("<h1>[salary]</h1>", ".*\\[.*\\].*")
    Thanks for your help!

    You must use Patterns and Matchers (java.util.regex.*).
    In short, what I'm looking for is a method something like String find(String text, String >regex) that would return "salary" when called as find("<h1>[salary]</h1>", ".*\\[.*\\].*")Let's
    String html
    be your input.
    Pattern p = Pattern.compile("(?im)<h1>(.*?)</h1>");
    Matcher m = p.matcher(html);
    if( m.find() ){
    // the whole matching substring is in m.group();
    // the string within the h1's is in m.group(1)
    }

  • Regex replace ^

    Hi trying to find a way of removing some text from a string where there is a ^ followed by either a number of character.  So I want to remove ^6 or ^h or whatever.  Trying the following:  var colorPattern:RegExp = /^[A-Za-z0-9]/g; match.replace(colorPattern,"");  I have tested the regex in a tester which seemed to work but this just returns match with the original text.  This is my first time using regex so any help would be appreciated.  Cheers Peter

    OK, figured it out. Here it is if anyone else needs to know
    <h1>(.+)</h1>
    <div id="background">
    replace with
    <div id="background">
    <h1>$1</h1>

  • Regex replacing multiple characters in string.

    I have been working through the Java regex tutorial and tried to modify one of the programs for my own use. Basically, I want to take a string and convert the chatracters A to T, T to A, C to G and G-C.
    I produced the rather crude program below, but of course it doesn't work. A could be converted to T and back again before the program terminates.
    I know that the code to do this correctly is probably quite complex, so could anyone point me in the direction of a tutorial which will help me to do this?
    This aside, I take it that if I am looking for multiple matches of characters which won't give the problem already indicated above, my code is too bloated anyway. Say, for example, instead of wanting to replace A to T, T to A, C to G and G-C, I wanted to replace dog-cat, horse-donkey - lion, tiger , cat-mouse. My code will work for this, but I am sure that it could be compressed a lot. Surely I would not need all the lines of code to do this?
    Thanks for any help,
    Tim
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    import java.io.*;  // needed for BufferedReader, InputStreamReader, etc.
        /** A Java program that demonstrates console based input and output. */
         class dna {
            // Create a single shared BufferedReader for keyboard input
            private static BufferedReader stdin =
                new BufferedReader( new InputStreamReader( System.in ) );
            // Program execution starts here
            public static void main ( String [] args ) throws IOException
                // Prompt the user
                System.out.print( "Type your DNA sequence: " );
                // Read a line of text from the user.
                String DNA = stdin.readLine();
                DNA = DNA.toUpperCase();
                String DNA2 = DNA;
                //calculate reverse complement
                Pattern A = Pattern.compile("A");
                Pattern T = Pattern.compile("T");
                Pattern C = Pattern.compile("C");
                Pattern G = Pattern.compile("G");
                Matcher AA = A.matcher(DNA);
                DNA = AA.replaceAll("T");
                Matcher TT = T.matcher(DNA);
                DNA = TT.replaceAll("A");
                Matcher CC = C.matcher(DNA);
                DNA = CC.replaceAll("G");
                Matcher GG = G.matcher(DNA);
                DNA = GG.replaceAll("C");
                // Display the input back to the user.
                System.out.println( "DNA input             : " + DNA2);
                System.out.println ("Complementary sequence: " + DNA);
        }

    TimM wrote:
    Thanks a lot!!! Can't believe you managed all that with so few lines of code.You're welcome.
    Must be great to know what you are doing :-)
    Thanks again,
    TimAfter being a bit more familiarised with the methods of String, you'll be able to do this in no time, I'm sure!

  • Applescript for replacing Original Files with aliases from an Organized Library.

    Hi, all I am new to Applescript and the support community. I am working on developing a script that takes a folder and replaces its original music content with aliases from an organized Library. Let me explain a little better.. I have a list of project folders that all contain folders and original music. My company uses iTunes to store and organize their music. Once iTunes has imported the music and has made a copy of the original file and organizes it accordingly we create an alias within the iTunes Library and manually drag and drop the aliases into our Project folders. I have a Script that reverts the aliases with the orginal file and the coding is
    Code starts here---
    set f to choose folder
    ProcessAlias(f as string)
    -- replacess all alias files in the passed folder path (string)
    on ProcessAlias(this_folder)
    -- get list of folders in this_folder
    set utid to AppleScript's text item delimiters -- store old delimiters
    set AppleScript's text item delimiters to {return}
    tell application "Finder"
    set folder_listing to every folder of folder this_folder as string
    end tell
    set AppleScript's text item delimiters to utid -- restore old delimiters
    if folder_listing is "" then
    set folder_list to {}
    else
    set folder_list to paragraphs of folder_listing
    end if
    -- process folder_list
    repeat with sub_folder in folder_list
    ProcessAlias(sub_folder) -- send each folder back to this subroutine
    end repeat
    -- get list of alias files and process them
    tell application "Finder"
    set alias_files to every file of folder this_folder whose kind is "Alias"
    repeat with this_alias in alias_files
    try -- error if broken alias
    set orig_item to original item of this_alias
    set item_dest to container of this_alias
    move orig_item to item_dest with replacing
    end try
    end repeat
    end tell
    return -- you can return a listing of moved originals if you want
    end ProcessAlias
    Basically I need the revers of this process. The objective is to save space and continue to let iTunes organize and copy files for us. I already have made a script that generates aliases into the subfolders of iTunes organized Library withour replacing the file that iTunes created. I also made a script that takes the itunes library and dumps all the aliases into a designated folder. I thought this may be helpful for drawing aliases to files that are in our project folder but seem to be stuck.
    I was thinking maybe if I took my iTunes and made all the aliases all into one folder to draw aliases from would make it easier. I'm not sure but it seems like there should be some easy way of doing this. Any suggestions would be greatly appreciated.

    Also Script above is for reverting aliases into original files as posted by Eatalay of this forum.

  • Need help with Regex replace in DW

    I'm converting a document to HTML that came with mixed case image filenames.  Our standards say that all filenames must be lowercase, so I used a CF script to convert them, but the src links in the HTML doc tag still shows the uppercase name.
    I planned to do something like this:
    Find:
    <img src="(.+?)" alt="(.+?)" />
    Replace:
    <img src="$1" alt="$2" />
    but I can't seem to get it to lowercase the $1 argument.  I've tried \L and all the normal things, but I just get the literal replacement.  Any tips?

    Use Regular Expressions
    "Big_Slick" <[email protected]> wrote in
    message
    news:f0ip6t$ee8$[email protected]..
    >I need to know how to Find/Replace for more than one tag
    at a time
    >(I.E--Search
    > for <table>, <tr>, and <td> or
    and and remove the code) As it is
    > right
    > now, it will only search for one tag at a time but I
    would assume DW can
    > handle
    > this kind of thing.
    >
    > Thanks in advance.
    >

  • Regex replacement syntax in editor

    Hello all,
    for the life of me I can't remember (nor find in the documentation) the syntax for doing string replacements using regular expressions in the CVI editor.
    Something like:
    Search: ({[~,]*},
    Replace: (\1, A10,
    Would change the string "(aei1," to "(aei1, A10,"
    But what are the delimiters to use ? {} like in my example ? And what's the variable ? \1 ? $1 ?
    I just can't find it.
    In sed syntax:
    sed -e "s/(\([^,]*\),/(\1, A10,/"

    No, it's not in there. Here's the relevant doc from sed for what I want:
           s/regexp/replacement/
                  Attempt to match regexp against the pattern space.  If successful, replace that portion  matched  with  replacement.   The replacement  may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.
    I'm sure I've used it with the CVI editor before.

  • Replacing multiple regexes at once

    Hi everybody,
    I'm trying to set up a regex replace that functions like this:
    String1 replaces String2
    String3 replaces String4
    and I'm curious, is there a way to set up a single call, like this:
    replaceAll( "[String1|String3]", "[String2|String4]" )
    where the one call can know which one to replace?
    I know it's possible to do:
    replaceAll( string1, string2 )
    replaceAll( string3, string4 )
    but I'm pretty sure I've seen this somewhere. Can anyone give me a hand with the syntax, if it's viable?
    Thanks,
    Jezzica85

    jezzica85 wrote:
    Thanks guys,
    I guess that means I'll have to try the old standby of one at a time. Hmm, I wonder where I thought I saw that? Maybe I dreamed it... :)
    Jezzica85You could do something like this:
    public static String multipleReplacements(String text,
            String[] patterns, String[] replacements) {
        // assume patterns.length == replacements.length
        for(int i = 0; i < patterns.length; i++) {
            text = text.replaceAll(patterns, replacements[i]);
    return text;

  • Search and replace all spaces between quotes with uderscore

    Hello,
    I'm new on Powershell and I'm trying to make the script that:
    searches over file and replaces all the spaces which have been found between quotes;
    removes all quotes (except these which has not value eg "").
    For example:
    Source file:
    string3=string4 string="string1 string2 string23" string8="" string5="string7 string8"
    Destination file:
    string3=string4 string=string1_string2_string23 string8="" string5=string7_string8
    I have been created script that searches the data correctly
    $file="c:\scripts\mk.txt"
    $data=Get-Content $file
    $1
    $regex = [regex]@'
    (?x) # ignore pattern whitespace option
    (?<test>(["'])(?:(?=(\\?))\2.)*?\1)
    $data |% {
    if ($_ -match $regex){
    new-object psobject -property @{
    test = $matches['test']
    }#when adding "|select-object test" I'm getting the correct data
    I have stopped here on search / replace operation (from space to underscore, and by removing the quotes leaving the empty quotes non touched). Can you help me to finish the script?
    Thanks!

    Try this.  It uses the [Regex] Replace static method, with a script block delegate:
    $file="c:\scripts\mk.txt"
    $data=Get-Content $file
    $regex = '(\S+=[^"\s]+)|(\S+="[^"]+")'
    $delegate = { $args[0].value.replace(' ','_') -replace '"(.+)"','$1' }
    $data |% { [regex]::Replace($_,$regex,$delegate) }
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

  • Search and replace, with pattern matching using a table

    I need to inspect a data stream and standardise a set of codes. I need to
    1. Match any patterns with a dash character and remove the dash and any following characters, eg BN-S -> BN, BN-SH -> BN, ARG-22 -> ARG, etc.
    2. Make a few specific word for word replacements, eg, PAEDSH -> PAED
    This is easy to hard code but can it be done using a table of regex substitutions? Can anyone give a pointer or link to some example code? The couple of regex replacement examples I've found use regex to locate but hard code substitutions.
    Thanks

    You could store all your patterns in a Map. Then iterate over the map inserting the patterns into a regex.

Maybe you are looking for