File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regex for replacing part of a pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regex for replacing part of a pattern" Watch "Regex for replacing part of a pattern" New topic
Author

Regex for replacing part of a pattern

Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
I have a requirement where i need to replace all '123' in a file by '456' except where it is prefixed and/or suffixed with any alphabet or number.

For e.g
123 becomes 456
/123 becomes /456
123/ becomes 456/
123a remains 123a
a123 remains a123
1234 remains 1234
4123 remains 4123

How should I do this?


Thanks and Regards
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Can you give some more details like what is the maximum size of the string that you are using. I mean can there be a string like this "/]123[{" or anything like that??

[Edit: added some help instead of just lectures]


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Mayur Somani
Greenhorn

Joined: Apr 14, 2008
Posts: 26

{Thanks Ankit for hint }
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Mayur Somani wrote:
{Thanks Ankit for hint }


Did you try for the input "123"


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Ankit Garg wrote:Can you give some more details like what is the maximum size of the string that you are using. I mean can there be a string like this "/]123[{" or anything like that??

[Edit: added some help instead of just lectures]


The string can be of any length. Not fixed. It may be like "123 /123 asfewf =123/ferwfew q1234re"
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Mayur Somani wrote:
{Thanks Ankit for hint }


This would make /123/ to 456 instead of /456/ which is not what is required.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18500
    
  40

Satya Maheshwari wrote:
This would make /123/ to 456 instead of /456/ which is not what is required.


Take a look at "zero width negative lookahead" and lookbehind, in the JavaDocs. These will match (or with negative, make sure to not match), yet, will not gobble the input that it is matching.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

replacing type of function will do...
not knowing much about API methods for parsers but i think string api can help

logic can be

if

String s.equals(123) then get the index of "123" that will give position in a string where '1' among "123" is stored so you can replcae that way

else,

check::

String replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement.

at String 5.0 API: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html


Dhruva
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

please check

Matcher API in java.regex package there few replacing methods given
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex for replacing part of a pattern
 
Similar Threads
How to write regular expression to skip a specific charaters in word
simple regex problem
Regular expression question
problem with method "validRoomNumber
String handling.