This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex for replacing part of a pattern

 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Mayur Somani
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

{Thanks Ankit for hint }
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mayur Somani wrote:
{Thanks Ankit for hint }


Did you try for the input "123"
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20900
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dhruva Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please check

Matcher API in java.regex package there few replacing methods given
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic