• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Regex for replacing part of a pattern

 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

{Thanks Ankit for hint }
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 67
Mac Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please check

Matcher API in java.regex package there few replacing methods given
 
Trust God, but always tether your camel... to this tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic