d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > New Line At The Beginning Of Regex?
Add Reply New Topic New Poll
Member
Posts: 4,554
Joined: Dec 1 2008
Gold: 0.50
May 9 2013 05:14pm
I have an unusual pattern I'm trying to replace. My regex looking like this,

private String newLine = System.getProperty("line.separator");
private String regex = newLine + " [0-100]-";

It's not working. Does anyone know why?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 9 2013 05:31pm
it looks good to me. Provide some sample input (as characters) so we can make sure the newline is correct. if you're using \r\n but the input string only has \n then obviously it won't work

This post was edited by carteblanche on May 9 2013 05:32pm
Member
Posts: 4,554
Joined: Dec 1 2008
Gold: 0.50
May 9 2013 05:51pm
Quote (carteblanche @ May 9 2013 06:31pm)
it looks good to me. Provide some sample input (as characters) so we can make sure the newline is correct. if you're using \r\n but the input string only has \n then obviously it won't work


Looks like this:

8 9
4 6
1 7
5-1
7 8
9
8 7
11-9


Where the " 5-1" and " 11-9" are what I'm trying to replace.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 9 2013 05:57pm
Quote (Furio @ May 9 2013 07:51pm)
Looks like this:

8 9
4 6
1 7
5-1
7 8
9
8 7
11-9


Where the " 5-1" and " 11-9" are what I'm trying to replace.


i think you missed the point of my request. show me the characters. eg break the string into a char[] and tell me what the newline is. is it \n, \r, \n\r, \r\n, etc and show me what System.getProperty("line.separator") is returning

what are you replacing it with? you might not need a regex depending on what you use. for example, you can possibly iterate line by line, trimming each entry, and replacing the - using the string's char replace method
Member
Posts: 4,554
Joined: Dec 1 2008
Gold: 0.50
May 9 2013 06:15pm
Quote (carteblanche @ May 9 2013 06:57pm)
i think you missed the point of my request. show me the characters. eg break the string into a char[] and tell me what the newline is. is it \n, \r, \n\r, \r\n, etc and show me what  System.getProperty("line.separator") is returning

what are you replacing it with? you might not need a regex depending on what you use. for example, you can possibly iterate line by line, trimming each entry, and replacing the - using the string's char replace method


My bad. I'm trying but the char array does not return \n, \t, \n\r, \r\n, it returns a new line followed by my regex. Like this,

[0-100]-

System.getProperty("line.separator") is returning a new line and nothing else.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 9 2013 06:20pm
i think you're still missing the point, so i'll rephrase: what unicode value is your input giving for newline and what unicode value is your regex using?
Member
Posts: 4,554
Joined: Dec 1 2008
Gold: 0.50
May 9 2013 06:29pm
Quote (carteblanche @ May 9 2013 07:20pm)
i think you're still missing the point, so i'll rephrase: what unicode value is your input giving for newline and what unicode value is your regex using?


Unicode for System.getProperty("line.separator") is 10.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 9 2013 06:42pm
and the newline for the input?

create a string "\n 5-11" and see if it works

i'm assuming here that you're using the regex properly. eg: via replace, and you're returning it since strings are immutable, etc. if you're just checking matches() or isMatch() or whatever the boolean is, obviously it'll fail since there's more to the string. and obviously something like mystringvar.replace(..) is gonna fail if you dont use the returned value

This post was edited by carteblanche on May 9 2013 06:46pm
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
May 10 2013 06:24am
The regex should be as simple as
Code
\d+-\d+

where \d is an integer, the + is 1 or more, the - of course is just a -
You don't need to check for new lines if the output looks like what you have above.

Try it out here: http://regexpal.com/
Member
Posts: 4,554
Joined: Dec 1 2008
Gold: 0.50
May 12 2013 11:02am
Quote (DirtyRasa @ May 10 2013 07:24am)
The regex should be as simple as
Code
\d+-\d+

where \d is an integer, the + is 1 or more, the - of course is just a -
You don't need to check for new lines if the output looks like what you have above.

Try it out here:  http://regexpal.com/


Actually that was extremely helpful. Thanks a lot!

Thanks to you as well carteblanche. I changed my newLine to just //n and escaped the second dash and it's working perfectly now.

This post was edited by Furio on May 12 2013 11:02am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll