Regular expression: dollar sign problem????

Hi:
Let's say I have a String like this:"add %a, %a, %b". I want to replace every "%a" with a "$t0". So I do the following:
public class testing {
public static void main(String[] args)
throws Exception {
Pattern p = Pattern.compile("%a");
Matcher m = p.matcher("add %a, %a, %b");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
while(result) {
m.appendReplacement(sb, "$t0");
result = m.find();
m.appendTail(sb);
System.out.println(sb.toString());
However, I got a group reference exception. It seems like dollar sign means group of words. How can I insert a dollar sign into a String without causing this excpetion?
Thanx a lot

From the api documentation for [url http://java.sun.com/j2se/1.4/docs/api/java/util/regex/Matcher.html#appendReplacement(java.lang.StringBuffer, java.lang.String)]Matcher.appendReplacement(java.lang.StringBuffer, java.lang.String):
The replacement string may contain references to subsequences captured during the
previous match: Each occurrence of $g will be replaced by the result of evaluating
group(g). If the second group matched the string "foo", for example, then passing
the replacement string "$2bar" would cause "foobar" to be appended to the string
buffer. A dollar sign ($) may be included as a literal in the replacement string by
preceding it with a backslash (\$).So, use m.appendReplacement(sb, "\\$t0") instead.

Similar Messages

  • Regular Expression in Java problem

    what is wrong with the following regex?
    query = query.replaceAll("SELECT.*?([WHERE.*?|GROUP BY.*?|HAVING.*?|ORDER BY.*?|LIMIT.*?]*?)","\\1");
    when I put in this:
    "SELECT WHERE MlsNumber=\'555555\', AdType=\'MyAdType\'"
    I get this:
    "1 WHERE MlsNumber='5100093', AdType='NytClass'"
    Where is the 1 coming from? I know the backreference must be working or I wouldnt get the WHERE statement back.

    There's a pretty good regex tutorial at this site: http://www.regular-expressions.info/ (I meant to include that in my first reply).
    Basically what I'm trying to do is cut the SELECT and
    anything after it up until it reaches the WHERE (and
    text), GROUPBY, HAVING, ORDER BY, or LIMIT. I want to
    remove The SELECT and text, but keep all instances of
    WHERE text GROUP BY text, etc. I also want to
    keep anything that is past then end of these
    expressions (in case there are option I haven't
    forseen).Try this:  query = query.replaceFirst("SELECT.*?(?=WHERE|GROUP BY|HAVING|ORDER BY|LIMIT)", "");The (?=...) part is a lookahead; it will cause the .*? to stop matching at the first instance of "WHERE", "GROUP BY", "HAVING", "ORDER BY", or "LIMIT". (Check out the "Lookahead & Lookbehind" section in the tutorial for an explanation.) However, if the first thing after the SELECT clause is one of the unknown options you mentioned, it will be removed too. If you know that keywords will always be in all caps, and that none of the other text will be, you could try generalizing the regex like this:  query = query.replaceFirst("SELECT.*? (?=[A-Z]{3,})", "");This regex assumes that any sequence of three or more capital letters preceded by a space is a keyword, which is probably not a safe assumption, but it gives you an idea of the kind of thing you can try.
    I thought the brackets were there to allow you to
    select choices from a group of items, if not I can
    remove them.Alternation doesn't require special bracketing characters. You usually want to enclose it in parentheses, but that's just to isolate it from the rest of the regex (e.g., "abc(?:foo|bar)xyz"). Square brackets are used for character classes, which are a completely different breed of animal; look them up in the tutorial.
    Apparantly the JDK docs are wrong, since they tell you
    to use the \\1 instead of $1 (which works).If you want to use a backreference within the regex, you use \1. For instance, if you want to match a complete HTML element, you might use  String regex = "<(\\w+)[^>]*>.*?</\\1>";But in the replacement string, you use $1. BTW, it's the Matcher docs that tell you that, not the Pattern docs.

  • CodeHints and dollar sign ($) problem

    Hi for everyone,
    I´m developing a extension to add CodeHints to the
    freeDOM.js (it´s a brazilian DOM Library) and I´m having
    problens when I use the dollar sing ($).
    I can (should) explain it better. Look:
    with that (part of) code:
    <menu pattern="$" DOCTYPES="JavaScript"
    caseSensitive="true">
    <menuitem label="$(strObjId, strObjId2, ...)" value="("
    icon="shared/mm/images/hintMisc.gif"/>
    everything is fine. So, when I type $ (dollar sing) the
    CodeHints appears with all menuitem that I configured.
    But, in this (part of) code:
    <function pattern="$(strObjId, strObjId2, ...) {}"
    doctypes="JavaScript" casesensitive="true" />
    <function pattern="foo_test(foo test string) {}"
    doctypes="JavaScript" casesensitive="true" />
    only the second menu tag appears when I typing the function
    in DW. So, my question is simples: Can I use the dollar sing ($) in
    the attribute pattern?
    Thanks

    Someone?

  • Regular Expressions (Perl style) problem...!!!

    I need to find the all appearance of html tags in string..
    string-->(fgsfdg<a href='dfhfdgdfg<b>dfghf<span>)
    Where a can find (download) this package.?

    You should be able to achieve this using the built in String methods. Say you want to replace each occurence of "http" w/ "foo".
    String httpString = "stringwithhttpinithttpafewhttptimeshttp";
    while(httpString.indexOf("http") > -1)
      httpString = httpString.subString(httpString.indexOf("http"), 4) +
        "foo" + httpString.subString(httpString.indexOf("http")+4, httpString.length());
    }My indices might be off by one, so you'd want to test that out, but hopefully that gives you an idea. This isn't the most efficient way to do it in java, but it's probably the simplest.
    -Derek

  • Regular expressions and input streams

    Hello,
    I am trying to find a way to read characters from a stream and find matches in them with regular expressions. The problem is that the stream may contain a big amount of characters, so I can't read all of them, keep them in a big string, and then perform the searches. Is there a way to perform searches while I read the characters? I scanned the documentation of Pattern and Matcher and found nothing that can help.
    Any ideas?
    Thanks.

    Looking at the method
    public Matcher Pattern.matcher(CharSequence input);
    one could be excused for assuming that one could turn an InputStream into a CharSequence object since a 'stream' implies sequencial access and CharSequence sounds like it provides sequential access.
    This would just require the implementation of 4 method of which two (toString() and subSequence()) could probably be ignored BUT the method you wouild have to implement are charAt() and length() which imply random access rather than sequencial access!
    Using the built in regex your problem looks difficult. If you are just looking for 'equality' matches (simple searching) then you could use Knuth-Morris-Pratt algorithm or the Boyer-Moore algorithm.

  • Find/replace and regular expression problem

    Hello, i'm using find and replace with a regular expression
    for the first time. I have it checkmarked and it's finding my text
    but it's missing (not highlighting) the ')' at the end of the line.
    Here's my code:
    [($[0-9]+<font size="-2">US</font>)]
    it's supposed to find everything inside the square brackets -
    but it misses the closing parenthesis after </font>. I need
    to find this string and replace with nothing to remove the string
    from any/all pages. Is there a reason why it's missing the closing
    parenthesis? I was actually able to add a few more parenthesis
    (e.g. "))))") before OR after the closing square bracket and it
    still found the original text minus the closing bracket and the
    extra parenthesis didn't prevent the text from being found.
    Any help is appreciated!
    James...

    WyattEA wrote:
    > Hello, i'm using find and replace with a regular express
    for the first time. I
    > have it checkmarked and it's finding my text but it's
    missing (not
    > highlighting) the ')' at the end of the line. Here's my
    code:
    >
    > [($[0-9]+<font size="-2">US</font>)]
    That's not how square brackets work
    Try:
    \(\$\d+<font size="-2">US</font>\)
    A left parens, followed by the dollar sign, followed by at
    least one
    digit, followed by <font size="-2">US</font>,
    followed by a right parens.
    Mick
    >
    > it's supposed to find everything inside the square
    brackets - but it misses
    > the closing parenthesis after </font>. I need to
    find this string and replace
    > with nothing to remove the string from any/all pages. Is
    there a reason why
    > it's missing the closing parenthesis? I was actually
    able to add a few more
    > parenthesis (e.g. "))))") before OR after the closing
    square bracket and it
    > still found the original text minus the closing bracket
    and the extra
    > parenthesis didn't prevent the text from being found.
    >
    > Any help is appreciated!
    >
    > James...
    >

  • Urgent!!! Problem in regular expression for matching braces

    Hi,
    For the example below, can I write a regular expression to store getting key, value pairs.
    example: ((abc def) (ghi jkl) (a ((b c) (d e))) (mno pqr) (a ((abc def))))
    in the above example
    abc is key & def is value
    ghi is key & jkl is value
    a is key & ((b c) (d e)) is value
    and so on.
    can anybody pls help me in resolving this problem using regular expressions...
    Thanks in advance

    "((key1 value1) (key2 value2) (key3 ((key4 value4)
    (key5 value5))) (key6 value6) (key7 ((key8 value8)
    (key9 value9))))"
    I want to write a regular expression in java to parse
    the above string and store the result in hash table
    as below
    key1 value1
    key2 value2
    key3 ((key4 value4) (key5 value5))
    key4 value4
    key5 value5
    key6 value6
    key7 ((key8 value8) (key9 value9))
    key8 value8
    key9 value9
    please let me know, if it is not possible with
    regular expressions the effective way of solving itYes, it is possible with a recursive regular expression.
    Unfortunately Java does not provide a recursive regular expression construct.
    $_ = "((key1 value1) (key2 value2) (key3 ((key4 value4) (key5 value5))) (key6 value6) (key7 ((key8 value8) (key9 value9))))";
    my $paren;
       $paren = qr/
               [^()]+  # Not parens
             |
               (??{ $paren })  # Another balanced group (not interpolated yet)
        /x;
    my $r = qr/^(.*?)\((\w+?) (\w+?|(??{$paren}))\)\s*(.*?)$/;
    while ($_) {
         match()
    # operates on $_
    sub match {
         my @v;
         @v = m/$r/;
         if (defined $v[3]) {
              $_ = $v[2];
              while (/\(/) {
                   match();
              print "\"",$v[1],"\" \"",$v[2],"\"";
              $_ = $v[0].$v[3];
         else { $_ = ""; }
    C:\usr\schodtt\src\java\forum\n00b\regex>perl recurse.pl
    "key1" "value1"
    "key2" "value2"
    "key4" "value4"
    "key5" "value5"
    "key3" "((key4 value4) (key5 value5))"
    "key6" "value6"
    "key8" "value8"
    "key9" "value9"
    "key7" "((key8 value8) (key9 value9))"
    C:\usr\schodtt\src\java\forum\n00b\regex>

  • Problem in creating a Regular Expression with gnu

    Hi All,
    iam trying to create a regular expression using gnu package api..
    gnu.regex.RE;
    i need to validate the browser's(MSIE) userAgent through my regular expression
    userAgent is like :First one ==> Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
    i wrote an regular expression like this:
    Mozilla.*(.*)\\s*(.*)compatible;\\s*MSIE(.*)\\s*(.*)([0-9]\\.[0-9])(.*);\\s*(.*)Windows(.*)\\s*NT(.*)\\s*5.0(.*)
    Actaully this is validating my userAgent and returns true, my problem is, it is returning true if userAgent is having more words at the end after Windows NT 5.0 like Second One ==> Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) Testing
    i want the regularExpression pattern to validate the First one and return true for it, and has to return false for the Second one..
    my code is:
    import gnu.regexp.*;
    import gnu.regexp.REException;
    public class TestRegexp
    public static boolean getUserAgentDetails(String userAgent)
         boolean isvalid = false;
         RE regexp = new RE("Mozilla.*(.*)\\s*(.*)compatible;\\s*MSIE(.*)\\s*(.*)([0-9]\\.[0-9])(.*);\\s*(.*)Windows(.*)\\s*NT(.*)\\s*5.0(.*)");
         isvalid = regexp.isMatch(userAgent);
         return isvalid;
    public static void main(String a[])
         String userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
         boolean regoutput = getUserAgentDetails(userAgent);
         System.out.println("***** regoutput is ****** " + regoutput);
    }please help me in solving this..
    Thanks in Advance..
    thanx,
    krishna

    Ofcourse, i can do comparision with simple string matching..
    but problem is the userAgent that i want to support is for all the MSIE versions ranging from 5.0 onwards, so there will the version difference of IE like MSIE 6.0..! or MSIE 5.5 some thing like that..
    any ways i will try with StringTokenizer once..!
    seems that will do my work..
    Thanks,
    krishna

  • Problems with java regular expressions

    Hi everybody,
    Could someone please help me sort out an issue with Java regular expressions? I have been using regular expressions in Python for years and I cannot figure out how to do what I am trying to do in Java.
    For example, I have this code in java:
    import java.util.regex.*;
    String text = "abc";
              Pattern p = Pattern.compile("(a)b(c)");
              Matcher m = p.matcher(text);
    if (m.matches())
                   int count = m.groupCount();
                   System.out.println("Groups found " + String.valueOf(count) );
                   for (int i = 0; i < count; i++)
                        System.out.println("group " + String.valueOf(i) + " " + m.group(i));
    My expectation is that group 0 would capture "abc", group 1 - "a" and group 2 - "c". Yet, I I get this:
    Groups found 2
    group 0 abc
    group 1 a
    I have tried other patterns and input text but the issue remains the same: no matter what, I cannot capture any paranthesized expression found in the pattern except for the first one. I tried the same example with Jakarta Regexp 1.5 and that works without any problems, I get what I expect.
    I am using Java 1.5.0 on Mac OS X 10.4.
    Thank to all who can help.

    paulcw wrote:
    If the group count is X, then there are X plus one groups to go through: 0 for the whole match, then 1 through X for the individual groups.It does seem confusing that the designers chose to exclude the zero-group from group count, but the documentation is clear.
    Matcher.groupCount():
    Group zero denotes the entire pattern by convention. It is not included in this count.

  • Regular Expressions - Problem

    Hi @ all,
    I need a complicate regular expression, I don´t know.
    I have a big folder with many .htm pages (800-1000) and I have to do the following:
    http://www.domain.de/ab%32-xyz?myshop=123
    I have to delete the "ab%32-xyz", the problem is, that in this are, there could be every symbol, letter or number.
    So the area between "http://www.domain.de/" and "?myshop=123" (these 2 areas are everytime identical in all documents) shoud be deleted.
    Could everyone say me, how to do this with regular expressions in dreamweaver?
    Thanks,
    Felix
    P.S.: Sorry, my Engish is not so good, I´m from Germany

    Do you want to replace the random text with anything?
    If not, this is how you do it in DW:
    Make a backup of the folder you want to edit, just in case anything goes wrong
    Edit > Find and Replace
    In the Find and Replace dialog box, select Folder from the "Find in" drop-down menu, and select the folder you want to work with.
    Select Source Code from the Search drop-down menu.
    Put the following code in the Find text area:
    (http://www\.domain\.de/)[^?]+(\?myshop=123)
    Put the following code in the Replace text area:
    $1$2
    In Options, select the "Use regular expression" check box.
    Click Replace All. Dreamweaver will warn you that the operation cannot be undone in pages that aren't currently open. As long as you have made a backup, click OK to perform the operation.

  • Regular Expression - Extract words before the PLUS Sign ?

    Dear All,
    I had many words with having a symbol plus. I need to extract the words before the plus sign.
    I can able to do this by using String.indexOf or String.contains. But i like to know is there is any way to do this using Regular Expression.
    sample string
    Kathire+san Output Kathire
    World+islike Output World
    Thanks,
    J.Kathir

    Here's one way.
    import java.util.regex.Pattern;
    String input = "abc+def";
    Pattern pat = pat.compile("\\+");
    String beforePlus = pat.split(input)[0];
    Sun's Regular Expression Tutorial for Java
    Regular-Expressions.info

  • Regular expression - find repeating +++ signs

    I'm trying to use regular expressions to remove duplicate +++ signs in a string. When I test my pattern using the expresso test (www.ultrapico.com) it parses the string correctly, in Java 1.5 it doesn't work. .. mp.matches() is always false. Any suggestions would be appreciated.
    finalLongstring = "TTL1,clip1+TTL2+++clip3,TTL4,clip4,TTL5,clip5+TTL6+clip6+TTL7+clip7,TTL8,clip8,TTL9,clip9,TTL10,clip10,TTL11,clip11,TTL12,clip12,TTL13,clip13+TTL14+clip14,TTL15,clip15,TTL16,clip16,TTL17,clip17,TTL18,clip18,TTL19,clip19,TTL20,clip20,TTL21,clip21,TTL22,clip22,TTL23,clip23,TTL24,clip24,TTL25,clip25,TTL26,clip26,TTL27,clip27,TTL28,clip28,TTL29,clip29";
    Pattern multiplePunctuation=null;
              multiplePunctuation=Pattern.compile("[,+]{2,6}");
              //                                     |  |
              //                                     |  2 or more times
              //                                     a comma or plus character
              Matcher mp=multiplePunctuation.matcher(finalLongstring);
              if(mp.matches()){
                   finalLongstring=mp.replaceAll("+");
    /code]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    Answere in your other thread.
    http://forum.java.sun.com/thread.jspa?threadID=5143654

  • Funny problem with schema pattern regular expression

    I have following element in my schema:
    <xsd:element name="lD">
         <xsd:simpleType>
              <xsd:restriction base="xsd:string">
                   <xsd:pattern value="\d{5,8}" />
              </xsd:restriction>
         </xsd:simpleType>
    </xsd:element>then in the XML, I have:
    <ID>
    123456789
    </ID>funny thing is the schema validates the value successfully. seems the upper bound of 8 the in the regular expression does not have any effect. however, 1234 would fail.
    i looked up the schema prime on W3C, it specifies that {n,m} means at least n and at most m.
    any idea anyone?
    I have using Xerces 1.4.4.
    Thanks

    I tried the example you mentioned and it seemed to
    work for me. It validated both the upper bound and
    the lower bound. Looking at the Manifest file in
    xerces jar, it looks like I have version 2.5.0yes, I know. Xerces 2 works, but Xerces 1 has the above problem. If you get Xerces 1.4.4 from xml.apache.org, you will see the problem.
    I can not use Xerces 2 in our production environment yet.

  • Interesting Regular Expression Problem

    Hi - I am fairly new to Java, but have some reg exp experience.
    Basically, I would like a regular expression to strip elements out of a text format. The elements are delimited by curly braces, very similar to Java. The problem is that the elements may contain other elements - the format is hierarchical. I need to extract the whole element, including its children.
    For example, I need to extract the B element from
    [A]
    a1     1
    a2     2
    b1     1
    b2     2
    [C]{
    c1     1
    c2     2}
    [C]{
    c1     3
    c2     4}}
    and the answer should be
    b1     1
    b2     2
    [C]{
    c1     1
    c2     2}
    [C]{
    c1     3
    c2     4}}
    The nature of the format is not fixed - I won't know how many child elements the B element contains.
    Can this be done using regular expressions, or do I have to write a custom string handling function?
    Thanks for any help.
    Mark

    :-) And I still don't understand it!It's quite easy though ;-) Suppose a word w is generated by some grammar.
    If this grammar is regular you can write word w as xyz where y is not the empty
    word and |x| < p. if w is generated by a regular grammar then xy^nz
    (n occurrences of y) can also be generated by the same grammar. That's
    the 'pumping' giggle part.
    Now suppose the language of nested parentheses is a regular language.
    All you have to do is find a word w = xyz where xy^nz is not part of the language.
    Let w= ((())) and x= ((, y= ( and z= ))), obviously xy^nz is not a properly nested
    parentheses word for n > 1. Note that every generated word in that language,
    long enough has to have that value p, where xy^nz |x| < p and xy^nz in the language.
    The pumping lemma giggle for context free languages is almost the same:
    you have to find two positions where the pumping fails/succeeds.
    kind regards,
    Jos (huhuh, he said 'pumping' ;-)

  • Regular expression and line break problem

    Hi friends,
    I was trying out one regular expression in java but, for some reasons its not working. The test string looks something like this in the eclipse watch window:-
    Test string:-*
    Content-Disposition: form-data; name="name"\n\nOmkar\n|
    In the above test string the last character is a pipe symbol.
    And the java code i have written is: -
    code:
        public static void getParameterValue(String paramName, String testParameter) {
                  String parameterRegex = "name=\""+paramName+"\"(.*)|";
                  Pattern pattern = Pattern.compile(parameterRegex,Pattern.DOTALL);
                  Matcher matcher = pattern.matcher(testParameter);
                  boolean isFound = matcher.find();
                  if (isFound) {
                       System.out.println("The parameter value is : "+matcher.group(1));
             }After, the debug line moves over the first line in this method, the value contained in the "parameterRegex" string variable is:-
    Regex:-*
    name="name"(.*)|
    However, my SOP statement, prints null....even though, "isFound" variable is true....can someone please explain me what could be the problem, where i am going wrong ?....
    I have even, set the DOTALL option for the regex.
    please some one help me!
    Thanks and Regards
    Omkar Patkar

    zomkar wrote:
    Sorry, folks, i did not mention the fact that i had posted the my query, in Java ranch also. But my problem lead to another problem of CROSS - POSTING !
    Apologies, for that !....but please, does any one have a solution to my regex ? .... any one ? please ?
    The value i get in paramaname is :- name
    which is present in double quotes in test string.
    is my regex incorrect ?The discussion at JavaRanch seems to be pretty alive, so why not leave this thread and continue there? That way, people won't have to first read two different threads on two different forums before trying to help you.

Maybe you are looking for

  • 2.0 Starter Edition Cannot Load Videos and Scanned Photos?

    We are using the Stater Edition 2.0 and has been working great, but we now can only retrieve photos, not videos and scanned photos from the camera since taking the HP to a computer geek. Using the browse settings for camera/card reader what do I need

  • Solaris FC Tape Drive

    Hi Folks, We have the following scenario: - SunFire v490 server - DAT 72 tape, SCSI - LTO4 tape, FC (via qlogic HBA) - Solaris 9 + updated patches We need some help regarding the FC tape configuration. *"cfgadm -al"* shows the FC tape as: c4::500110a

  • Dirty transaction check.

    Experts, Working in jdev 11.1.1.3.0 i want to check dirty transaction on button click. I have tried with ControllerContext cctx = ControllerContext.getInstance(); if (!(cctx.getCurrentViewPort().getTaskFlowContext().isDataDirty())) and DCBindingConta

  • Which version of jdeveloper has Search option in view editor of VO ?

    Hi Can anybody tell me which version of jdeveloper has Search option in view editor of VO ?

  • 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