EmadGirgis
asked on
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.
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.
ASKER
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
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
Not entirely clear what you want to match (would have been good to post an example) but try
s = s.replaceAll("--\\w*?\n", " ");
ASKER
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
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
ASKER
Is your solution is still valid based on the example I gave
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
<<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
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
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
:)
or
str.replaceAll("\\n+", "\\s");
str.replaceAll("\\-\\-", "\\s");