• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Java regular expressions...

Hi,

I have string. The string has multiple lines in it.. I mean it contain \n.

Now I need to replace every occurrence of -- and any number of letters and  \n by a space
again the pattern is
minus minus followed by any number of characters and ending by new line
this pattern needs to be replace by a single space

I am new for regular expressions and java.

Can you please point to me also which packages I need to import. I hope you provide an example. You can call the String str or something.
0
EmadGirgis
Asked:
EmadGirgis
  • 3
  • 3
  • 3
2 Solutions
 
Gurvinder Pal SinghCommented:
str.replaceAll("(\\n+|\\-\\-)", "\\s");
or
str.replaceAll("\\n+", "\\s");
str.replaceAll("\\-\\-", "\\s");
0
 
EmadGirgisAuthor Commented:
str.replaceAll("(\\n+|\\-\\-)", "\\s");

My issue is the -- comes before the newline
What here represents zero or many characters between both

is the \\s replaces that with a space.

What are the imports that I need to have


What type is str
is this regular string
0
 
CEHJCommented:
Not entirely clear what you want to match (would have been good to post an example) but try
s = s.replaceAll("--\\w*?\n", " ");

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
EmadGirgisAuthor Commented:
CHEJ,

Here is a text example

-- This select statement would get active products
Select
Product_ID ,  --Uniqe id
Product_Name
from  product where product_active = 1;

I need to get rid of -- This select statement would get active products and
 --Uniqe id
0
 
EmadGirgisAuthor Commented:
Is your solution is still valid based on the example I gave
0
 
CEHJCommented:
In that case, try simply
s = s.replaceAll("--.*?\n", " ");

Open in new window

0
 
Gurvinder Pal SinghCommented:
<<What are the imports that I need to have>>
none

<<What type is str
is this regular string>>
Yes

<<My issue is the -- comes before the newline
What here represents zero or many characters between both>>

Actually order doesn't matter. I have given '|' in the regular expression, which means whenever either of \\n or \\-\\- comes, it will be replaced by space \\s
0
 
Gurvinder Pal SinghCommented:
actually it needs to be

str.replaceAll("(\\n+|\\-\\-)", " ");
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now