• Post Reply Bookmark Topic Watch Topic
  • New Topic

regular Expression in java  RSS feed

 
premchand Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to remove all characters between /* and */ in java.

for Example, if the line is like this :

/* 16: */ public String toString()
i should get the output like this
public String toString()

Could some one please let me know the "Expression" in java that can be used with String.replaceAll.


Thanks in advance :
Premchand.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about /** 16: */?
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about /* 16: */ */?
 
premchand Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried with below code :

line.replaceAll("/* (.*?) */", " ");
line.replaceAll("\\*.*?/", " ");

but it is not working this code..
 
premchand Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/* 16: */

This code will be removed at all from the file.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first has slash-star, but the star is a metacharacter needing escaping.
The second misses out the slash at the beginning.
In the middle, why do you have the ? after .*? You also have spaces before and after the middle part. Why?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
premchand Singh wrote:/* 16: */

This code will be removed at all from the file.
Which code? What about the two alternatives SvH and I mentioned?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
premchand Singh wrote:Hi,
I want to remove all characters between /* and */ in java.

for Example, if the line is like this :

/* 16: */ public String toString()
i should get the output like this
public String toString()


Your example does not match your specs. You say you want to remove the characters BETWEEN "/*" and "*/". that sounds like you want to leave the "/*" and "*/", but your example takes them away.

Writing clear, precise specs is VERY important. Incorrect specs leads to incorrect code.
 
premchand Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let me become clear on this :

suppose i have code like this :

/* 16: */ public String toString()

i want output like as below :

public String toString()

I have tried the regular expression mentioned in the chain..

i hope this should clarify the question :
 
premchand Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the answer :

This simple statement worked for me :

line.replaceAll("/.*?/", "")

Thanks everybody who tried to help me
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:In the middle, why do you have the ? after .*?

This makes the * non-greedy, which is important if you want to correctly match this string:

"/* 16: */ keep this part /* keep this comment */"
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
premchand Singh wrote:I got the answer :

This simple statement worked for me :

line.replaceAll("/.*?/", "")

Thanks everybody who tried to help me


But what about this line?

int foo = a / b / c;
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
premchand Singh wrote:I got the answer :

This simple statement worked for me :

line.replaceAll("/.*?/", "")


How about trying that simple statement on ...

int result = 5 / 3 + 4 + 5 + 6 * 7 * 8 / 9;

Henry
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote: . . .
This makes the * non-greedy, . . .
Thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!