File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes RegularExpression for string replacement Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "RegularExpression for string replacement" Watch "RegularExpression for string replacement" New topic
Author

RegularExpression for string replacement

Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
Hi

I need one regular expression for this requirement.
I have to search for & character which is not coming before test;,in that case replace the & with &test;
Example:
input string "&abc&test;"
output string "&test;abc&test;"

input: "abc&test;"
output: "abc&test;"

input: "abc&g&test;"
output: "abc&test;g&test;"

please help me in writing the regular expression

[ May 14, 2008: Message edited by: Gunjan Kumar ]

[ May 14, 2008: Message edited by: Gunjan Kumar ]
[ May 14, 2008: Message edited by: Gunjan Kumar ]

Gunjan Kumar
SCJP 1.5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
Not easy. Please tell us what you have tried so far, and whether it works.
Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
I have tried like this,but not able to achieve the desired output
i am not able to create a correct reg exp

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4201
    
    3

From your problem description, you need a regex that
-- matches the single character "&"
-- with a non-capturing zero-width negative lookahead for "test;"

And replace with "&test;"

Please go through the API for java.util.regex.Pattern and try to figure out something for yourself. Come back with your best efforts if you are unsuccessful.

edit Saw your code after I had posted that. All you need is

[ May 14, 2008: Message edited by: Darryl Burke ]

luck, db
There are no new questions, but there may be new answers.
Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
Thanks for your reply

with a non-capturing zero-width negative lookahead for "test;"

What do you mean by non-capturing zero-width negative?
Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
Thanks for your reply
it works for me
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4201
    
    3

Originally posted by Darryl Burke:
From your problem description, you need a regex that
-- matches the single character "&"
-- with a non-capturing zero-width negative lookahead for "test;"

And replace with "&test;"

Please go through the API for java.util.regex.Pattern and try to figure out something for yourself. Come back with your best efforts if you are unsuccessful.

edit Saw your code after I had posted that. All you need is


[ May 14, 2008: Message edited by: Darryl Burke ]


edit2: Code gets replaced by smilies?
That was "&(?!test-semicolon-)"
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
Originally posted by Darryl Burke:


edit2: Code gets replaced by smilies?
That was "&(?!test-semicolon-)"


Check the 'Disable smilies in this post' check box at below left if you want to avoid this problem.


Joanne
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4201
    
    3

Originally posted by Gunjan Kumar:
What do you mean by non-capturing zero-width negative?


I repeat, please read the API for Pattern. Also The Java� Tutorial: Lesson: Regular Expressions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4201
    
    3

Originally posted by Joanne Neal:


Check the 'Disable smilies in this post' check box at below left if you want to avoid this problem.


Thanks for the heads-up, Joanne. @800 X 600, I hadn't ever scrolled down enough to notice it.

cheers, Darryl
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RegularExpression for string replacement
 
Similar Threads
split()...
Problem in StringTokenizer
Unable to match a regex
Need help in creating reg expression
java regular expression problem