Match beginning of line with Regular Expression

I'm confused about dreamweaver's treatment of the characters
^ and $ (beginning of line, end of line) in regex searches. It
seems that these characters match the beginning of the file, not
the beginning of the various lines in the file. I would expect it
to work the other way around. A search like:
(^.)
should match every line in the file, so that a find/replace
could be performed at the beginning of each line, like this:
HELLO$1
which would add 'HELLO' at the start of each line in the
file.
Instead, this action only matches the first character of the
file, sticks 'HELLO' in front of it, and then quits (or moves on to
the next file). The endline character $ behaves in a similar
fashion, matching only the end of the file, not the end of each
line.
I've searched, and all the literature about regular
expressions in dreamweaver seems to indicate that I'm expecting the
correct behavior:
www.adobe.com/devnet/dreamweaver/articles/regular_expressions_03.html
quote:
^ Beginning of input or line ^T matches "T" in "This good
earth" but not in "Uncle Tom's Cabin"
$ End of input or line h$ matches "h" in "teach" but not in
"teacher"
Thanks for any insight, folks.

Hi Winston,
I am still digesting the material from the regular expression book and will take sometime to become proficient with it.
It seems that using groupCount() to eliminate the unwanted text does not work in this case, since all the lines returned the same value. Ie 3 posted earlier. This may be because the patterns are complex and only a few were grouped together. Otherwise, could you provide an example using the string posted as opposed to a hyperthetic one. In the meantime, at least one solution have been found by defining an additional special pattern “\\A[^%].*\\Z”, before combining / intersecting both existing and the new special pattern to get the best of both world. Another approach that should also work is to evaluate the size of String.split() and only accept those lines with a minimum number of tokens.
Anyhow, I have come a crossed another minor stumbling block in the mean time with the following line, where some hidden characters is preventing the existing pattern from reading it:
o;?Mervan Bay 40 Boyde St 7 br t $250,000 X West Park AE
Below is the existing regular expression that works for other lines with the same pattern but not for special hidden characters such as “o;?”:
\\A([A-Z][a-z]*){1,2} [0-9]{0,4}/?[0-9]{0,4}-?[0-9]{0,4} ([A-Z][a-z]*){1,2} St|Rd|Av|Sq|Cl|Pl|Cr|Gr|Dr|Hwy|Pde|Wy|La [0-9] br [h|u|t] \\$\\d+,\\d+|\\$\\d*\\,\\d+,\\d+ ([A-Z][a-z]*){1,}\\ZIs it possible to come up with a regular expression to ignore them so that this line could be picked up? Would also like to know whether I could combine both the special pattern “\\A[^%].*\\Z” with existing one as opposed to using 2 separate patterns altogether?
Many thanks,
Jack

Similar Messages

  • Need advice on negating a whole string line with regular expression

    Hi All,
    I am not able to ignore / get rid of the following line even though my Java 6 (Windows XP) String Pattern matching has not taken cater for it:
    *% Cleared: 61%*
    Below is the existing Java String Pattern matching in the simple program:
    Pattern pattern = Pattern.compile("(^.*[A-Z][a-z]*){1,2} \\d{0,4}/?\\d{0,4} ([A-Z][a-z]*){1,2} St|Rd|Av|Sq|Cl|Pl|Cr|Gr|Dr|Hwy|Pde|Wy|La \\d br [h|u|t] \\$\\d+,\\d+|\\$\\d*\\,\\d+,\\d+ ([A-Z][a-z]*){1,}.*$");This pattern is working for valid strings.
    The following pattern has included "^(?!.*\.\.).*$" into the existing one but had no luck still:
    Pattern pattern = Pattern.compile("^(?!.*\.\.).*$|((^.*[A-Z][a-z]*){1,2} \\d{0,4}/?\\d{0,4} ([A-Z][a-z]*){1,2} St|Rd|Av|Sq|Cl|Pl|Cr|Gr|Dr|Hwy|Pde|Wy|La \\d br [h|u|t] \\$\\d+,\\d+|\\$\\d*\\,\\d+,\\d+ ([A-Z][a-z]*){1,}.*$)");This picked up other rubbish including "*% Cleared: 61%*".
    I am looking for a single regular expression that applies to the whole line.
    I am quite new to regular expression but has read through Regular Expressions Cookbook (Oreilly - 2009) and is still not familiar with advance functions such as lookahead / lookbehind...
    Your assistance would be appreciated.
    Thanks,
    Jack

    Hi Winston,
    I am still digesting the material from the regular expression book and will take sometime to become proficient with it.
    It seems that using groupCount() to eliminate the unwanted text does not work in this case, since all the lines returned the same value. Ie 3 posted earlier. This may be because the patterns are complex and only a few were grouped together. Otherwise, could you provide an example using the string posted as opposed to a hyperthetic one. In the meantime, at least one solution have been found by defining an additional special pattern “\\A[^%].*\\Z”, before combining / intersecting both existing and the new special pattern to get the best of both world. Another approach that should also work is to evaluate the size of String.split() and only accept those lines with a minimum number of tokens.
    Anyhow, I have come a crossed another minor stumbling block in the mean time with the following line, where some hidden characters is preventing the existing pattern from reading it:
    o;?Mervan Bay 40 Boyde St 7 br t $250,000 X West Park AE
    Below is the existing regular expression that works for other lines with the same pattern but not for special hidden characters such as “o;?”:
    \\A([A-Z][a-z]*){1,2} [0-9]{0,4}/?[0-9]{0,4}-?[0-9]{0,4} ([A-Z][a-z]*){1,2} St|Rd|Av|Sq|Cl|Pl|Cr|Gr|Dr|Hwy|Pde|Wy|La [0-9] br [h|u|t] \\$\\d+,\\d+|\\$\\d*\\,\\d+,\\d+ ([A-Z][a-z]*){1,}\\ZIs it possible to come up with a regular expression to ignore them so that this line could be picked up? Would also like to know whether I could combine both the special pattern “\\A[^%].*\\Z” with existing one as opposed to using 2 separate patterns altogether?
    Many thanks,
    Jack

  • Command line style regular expression string parser

    Hi people,
    I am currently working on a program where I need to parse a file (or any input stream) line by line. I then need to parse every line for arguments. Each line is formatted similar to how arguments are passed to the command line. The regular expression needs to split every line by any encountered whitespace, but needs to be able to retain any whitespace within double quotes (i.e. "some spaced text here"). Arguments can be numbers, booleans and (quoted) strings. Quoted strings must also be able to have escaped quotes in it (as below). The quotes for the quoted string (the outer ones, obviously not the escaped ones) do not necessarily have to be retained.
    An example input line:
    arg1 arg2 "arg 3" "arg 4" 987 arg6 "arg \"arg \"arg 7"
    Desired example output:
    arg1
    arg2
    arg 3
    arg 4
    987
    arg6
    arg "arg "arg 7
    After the input line has been split up the program will handle any parsing (i.e. numbers, booleans, etc.). The program currently uses a simple for loop to iterate over all characters in the line and splits it up appropriately by checking every character. However, if this can be done automatically by using a regular expression passed to String.split() (or with some use of the regex package), it would remove quite a bit of redunant code and make the program that much more maintainable.
    I do not have much experience with regular expressions since I have never really had the need to use them, but if they can work in this case it would be great.
    Thanks in advance for any help.

    Almost any parsing problem can be solved if you throw a big enough and ugly enough regex at it, or so I'm told.
    I think what you are doing is also amenable to java.io.StreamTokenizer:
    import java.io.*;
    import static java.io.StreamTokenizer.*;
    public class StreamTokenizerExample {
        public static void main(String[] args) throws IOException {
            StringReader input = new StringReader("arg1 arg2 \"arg 3\" \"arg 4\" 987 arg6 \"arg \\\"arg\\\" arg 7\"\nnextline");
            StreamTokenizer in = new StreamTokenizer(input);
            in.eolIsSignificant(true);
            for(int ttype; (ttype = in.nextToken()) != TT_EOF; ) {
                switch (ttype) {
                    case TT_WORD:
                        System.out.println("String[" + in.sval + "]");
                        break;
                    case TT_NUMBER:
                        System.out.println("number[" + in.nval + "]");
                        break;
                    case TT_EOL:
                        System.out.println("[EOL]");
                        break;
                    case '"':
                        System.out.println("quoted[" + in.sval + "]");
                        break;
                    default:
                        System.out.println("unexpected " + ttype);
    {code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • How to search with regular expression

    I make pdx files so that I can search text quickly. But Acrobat doesn't provide a way to search with regular expression. I'm wondering if there is a way that I don't know to search for regular expression in Acrobat Pro 9?

    First, Acrobat must "mount" the PDX.
    As "Find" does not use the cataloged index, use Shift+Ctrl+F to open the advanced search dialog.
    It may be helpful to first enter Acrobat Preferences and for the Search category tick "Always use advanced search options".
    Back to the Search dialog - use the drop down menu for "Look In" to pick "Select Index" then, if no PDXs show, click the Add button.
    In the Open Index File dialog, browse to the location of the desired PDX and select it.
    OK out and use "Return results containing" to pick a "Match ..." requirement or Boolean.
    To become familiar with query syntax, for Acrobat, it is good to review Acrobat Help.
    http://help.adobe.com/en_US/Acrobat/9.0/Professional/WS58a04a822e3e50102bd615109794195ff-7 c4b.w.html
    Be well...

  • Problem with Regular Expression

    Hi There!!
    I have a problem with regular expression. I want to validate that one word and second word are same. For that I have written a regex
    Pattern p=Pattern.compile("([a-z][a-zA-Z]*)\\s\1");
    Matcher m=p.matcher("nikhil nikhil");
    boolean t=m.matches();
    if (t)
              System.out.println("There is a match");
         else
              System.out.println("There is no match");
    The result I am getting is always "There is no match
    Your timely help will be much appreciated.
    Regards

    Ram wrote:
    ErasP wrote:
    You are missing a backward slash in the regex
    Pattern p = Pattern.compile("([a-z][a-zA-Z]*)\\s\\1");
    But this will fail in this case.
    Matcher m = p.matcher("Nikhil Nikhil");It is the reason for that *[a-z]*.The OP had [a-z][a-zA-Z]* in his code, so presumably he know what that means and wants that String not to match.

  • Grouping & Back-references with regular expressions on Replace Text window

    I really appreciate the inclusion of the Regular Expressions in the search & replace feature. One thing I am missing is back-references in the replacement expression. For instance, in the unix tools vi or sed, I might do something like this:
    s/\(firstPart\) \(secondPart\) \(oldThirdPart\)/\2 \1 newThirdPart/g
    which would allow me to switch the places of firstPart and secondPart, and totally replace thirdPart. If grouping and back-references are already present in the Replace Text window, how does one correctly invoke them?

    duplicate of Grouping & Back-references with regular expressions on Replace Text window

  • Assistance with Regular Expression and Tcl

    Assistance with Regular Expression and Tcl
    Hello Everyone,
      I recently began learning Tcl to develop scripts for automating network switch deployments. 
    In my script, I want to name the device with a location and the last three octets of the base mac address.
    I can get the Base MAC address by : 
    show version | include Base
     Base ethernet MAC Address       : 00:00:00:DB:CE:00
    And I can get the last three octets of the MAC address using the following regular expression. 
    ([0-9a-f]{2}[:-]){2}([0-9a-f]{2}$)
    But I have not been able to figure out how to call the regular expression in the tcl script.
    I have checked several resources but have not been able to figure it out.  Suggestions?
    Ultimately, I want to set the last three octets to a variable (something like below) and then call the variable when I name the switch.
    set mac [exec "sh version | i Base"] (include the regular expression)
    ios_config "hostname location$mac"
    Thanks for any assistance in advance.
    Chris

    This worked for me.
    Switch_1(tcl)#set result [exec show ver | inc Base]   
    Base ethernet MAC Address       : 00:1B:D4:F8:B1:80
    Switch_1(tcl)#regexp {([0-9A-F:]{8}\r)} $result -> mac
    1
    Switch_1(tcl)#puts $mac                               
    F8:B1:80
    Switch_1(tcl)#ios_config "hostname location$mac"      
    %Warning! Hostname should contain at least one alphabet or '-' or '_' character
    locationF8:B1:80(tcl)#

  • Get the string between li tags, with regular expression

    I have a unordered list, and I want to store all the strings between the li tags (<li>.?</li>)in an array:
    <ul>
    <li>This is String One</li>
    <li>This is String Two</li>
    <li>This is String Three</li>
    </ul>
    This is what have so far:
    <li>(.*?)</li>
    but it is not correct, I only want the string without the li tags.
    Thanks.

    No one?
    Anoyone here experienced with Regular Expression?

  • Troubles with regular expressions

    I seem to be having problems writing my regular expression. I check it on a regex testing website and it should work. Maybe I am doing something wrong because it is in Java and not Perl? I was wondering if someone could give me a little help?
    Pattern p = Pattern.compile("<.*posttitle.*h2>", Pattern.DOTALL);
            Matcher m = p.matcher(content);
            boolean b = m.matches();That's what my regular expression looks like but the boolean b keeps returning false. The text I am looking at is the source code of [dailypoetryclub.com|http://dailypoetryclub.com] and I want to basically pull out just the links of each day's topic. Any ideas what I'm doing wrong?

    1) Sounds like you need find() not matches() (matches look for the whole string to match exactly) if you are looking for the pattern in a long string.
    2) You should make the ".*" reluctant or it will take more than you expect.

  • XML node name matching with regular expressions

    Hello,
    If i have an xml file that has the following:
         <parameter>
              <name>M2-WIDTH</name>
              <value column="09" date="2004-10-31T19:56:30" row="03" waferID="PUK444150-20">10.4518</value>
         </parameter>
         <parameter>
              <name>M2-GAP</name>
              <value column="29" date="2004-10-31T19:56:30" row="06" waferID="PUK444150-03">2.864</value>
         </parameter>
         <parameter>
              <name>RES-LENGTH</name>
              <value column="29" date="2004-10-31T19:56:30" row="06" waferID="PUK444150-03">2.864</value>
         </parameter>
    Is there anyway i can get a list of nodes that match a certain pattern say where name=M2* ?
    I cant seem to find any information where i can match a regular expression. I see how you can do:
    String expression=/parameter[@name='M2-LENG']/value/text()";
    NodeList nodes = (NodeList) xPath.evaluate(expression, inputSource, XPathConstants.NODESET);
    But i want to be able to say:
    String expression=/parameter[@name='M2-*']/value/text()";
    Is this possible? if so how can i do this?
    Thanks!

    As implemented in Java, XPath does not support regular expressions, but in most cases there are workarounds thanks to XPath functions. Correct me if I'm wrong, but setting your expression against the XML document (i.e. because there are no "name" attributes in the whole document) I think you mean to get the value of the <value> elements that have a <parameter> parent element and a <name> sibling element whose value starts with "M2-". If that is the case, you can use the following query expression:String expression = "//parameter/value[substring(../name,1,3)='M2-']";Sorry if I misunderstood the meaning of your expression, but I hope this will help you get the hang of using XPath functions as a substitute for regular expressions.

  • Matching Quotes With Regular Expressions

    Hi, I have been attempting to develop an app which extract texts from pdfs then applies regular expression to the text. In one instance I attempt to match a curly open quote symbol which matches the . construct as well as the \W. However, it does not match \p{Punct} or \p{P} - does anyone know why this might be?
    I have read that a pattern \p{Pi} exists for matching opening brackets but am told when I run Java that no such characters class exists - is there anywhere where I can get info on all of the character classes available (not the Pattern javadoc)?
    Thanks very much,
    Ross

    . construct match anything and \W matches any non alphanumaric
    to match a curly bracket use \{
    Basicaly ( { [  has special meanings in regex so when you match them you have to use \{, \(,  \[  in java strings it means \\{, \\(, \\[                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Match leading spaces in a regular expression?

    I have the following regular expression:
    ^[^0-9A-Z][^0-9A-Z ]$
    For some reason, it does not match leading spaces, so " A" is considered no match, even though space is obviously neither a digit or an uppercase letter...
    [Edit] Also, it doesn't seem to match case properly - "ab" is matched, but not "Ab" or "aB"...

    Then remove the not's ^
    From : ^[^0-9A-Z][^0-9A-Z ]$
    To     : ^[0-9A-Z][0-9A-Z ]$
    The first ^ indicates the beginning of the line and $ the end of the line.  The pattern will only work with two characters on the line.  If you have more than two characters on the line then use this
    To     : ^[0-9A-Z][0-9A-Z ]
    jdweng

  • Searching in reverse with regular expressions

    I have recently constructed a "Find" dialog for my editor and would like to support the use of regular expressions (for people who know more about them than I do.) I have radio buttons that allow the user to search down (from the current caret position to the end of the document) or up (from the current caret position to the beginning of the document.)
    I have it working fine with literal text via String.indexOf() and String.lastIndexOf(), but I'm not sure how to implement an "upwards" search using regular expressions. It seems that java.util.regex only provides for searching from the current caret position downwards. Any suggestions?

    two not-very-good-for-big-document suggestions
    1) search up by searching down (from the beginning) to the last match before the position searched from
    2) when the user asks for a search find all matches, number them and keep the index in the doc where they were found, then look up the largest index less than the carets index
    you might try constructing succesively larger strings that end at the current caret position, and matching them one by one. If you're near the bottom, and theres no matches, this is also likely to be sloow..
    asjf

  • Replace All with Regular Expression

    Hi all,
    I need a help to replace the String:
    to
    "<a href=\"1\"">Java Programming</a>"
    I was trying to replace with
    .replaceAll("\\[\\[*([^\\]]*?)*\\]\\]", "<a href=\"\"></a>")
    but I don't know how to separate the parameters values.
    Best regards                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    Hi prometheuzz,
    you are the one!!
    But how I could do to a Srting like this:
    Ah, more requirements...
    Things are getting a bit messy now:
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    class Main { 
        public static void main(String[] args) {
            String line = "any text any text any text any text any text. "+
                          "Click - [[Code: 6 Title: Java Programming]]. "+
                          "any text any text any text any text any text. "+
                          "- [[C�digo: 2 T�tulo: Sun]] text text "+
                          "any text any text any text any text any text.";
            String newLine = line;
            String[] wikiTags = getWikiTags(newLine);
            for(String tag : wikiTags) {
                String newTag = "<a href=\""+get("(Code:|C�digo:)", " ", tag)+
                                "\">"+get("(Title:|T�tulo:)", "$", tag)+"</a>";
                newLine = newLine.replaceFirst("\\[\\["+tag+"\\]\\]", newTag);
            System.out.println(line);
            System.out.println(newLine);
        public static String[] getWikiTags(String text) {
            java.util.List<String> list = new java.util.ArrayList<String>();
            Pattern pattern = Pattern.compile("(?<=\\[\\[)(.*?)(?=\\]\\])");
            Matcher matcher = pattern.matcher(text);
            while(matcher.find()) {
                list.add(matcher.group());
            return list.toArray(new String[list.size()]);
        public static String get(String start, String end, String text) {
            Pattern pattern = Pattern.compile("(?<="+start+"\\s)(.*?)(?="+end+")");
            Matcher matcher = pattern.matcher(text);
            return matcher.find() ? matcher.group() : "#ERROR#";
    }Details about regex:
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
    http://www.regular-expressions.info/java.html
    http://java.sun.com/docs/books/tutorial/essential/regex/
    >
    Thanks a lot for you helpLike I said, it's a bit of a messy solution. See if you can use (a part of) it.
    Good luck.

  • DOM Parser fails with regular expression using anchor (carat, dollar)

    I'm using version "Oracle XDK Java 9.0.4.0.0 Production"
    In trying to parse XML against schema: a regular expression fails to parse the data "8:00" with the following simple regular expression: "^.*$" (used to narrow the error)
    The error message is
    <Line 14, Column 25>: XSD-2025: (Error) Invalid text '8:00' in element: 'XYZ'
    If I remove the anchors and just have ".*", the data is parsed successfully.
    I dont understand why the parse fails when I use a anchors in the regular expression, and the java Pattern/Matcher classes succeed with the anchors?

    That "ns670" string is an xml namespace prefix. it should have a corresponding xml namespace declaration somewhere in the xml document (i'm guessing you have not shown the whole document). the actual value of an xml namespace prefix is meaningless. if you parse the xml with a namespace aware DOM parser, it should generate Nodes with the correct namespace. the namespace is the value you care about when extracting data from the document, not the namespace prefix.
    alternately, if you parse the document using a namespace aware DOM parser, you can just look for nodes based on their "local" name (the part after the ":" separator) and ignore the namespace/prefix.
    whatever you do, please do not parse the xml with a regex, see this http://stackoverflow.com/a/1732454/552759 for details (applies to xml as well).

Maybe you are looking for