There are too many ways to do this. Is that the content in each line?
("[^BitTorrent]+([0-9]{1,5}\.[0-9]{1,2})");
Your regex is incorrect when you use [] in regex because anything inside [] means "or" for any character inside it. However, the symbol ^ inside [] means "not" and that means you are looking for any combination of i|T|o|r|e|n|t but not 'B'.
"^.+(\\d+\.\\d+)\\s*Kbps\\s*$" means anything from start but group only the end digits with precision value and end with Kbps.
The one with "BitTorrent" could be "^Bit.+(\\d+\.\\d+)\s*\Kbps\\s*$"
The one with 6881 could be "^[A-Za-z\\s]+6881\\s+.+(\\d+\.\\d+)\\s*\Kbps\\s*$"
PS: I believe regex string in Java needs double backslash for escaping?
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
is it nessasary to use regular expression concept to get your actual output?
may i suggest any alternate solution to your requirement?
isn't it good to use split() for this requirement?
What do you think split() uses?
Here's its signature:
public String[] split(String regex)
API:
Splits this string around matches of the given regular expression.
mvmalderen
Posting Maven
2,612 posts since Feb 2009
Reputation Points: 2,221
Solved Threads: 280
Skill Endorsements: 36
I am wrong about explanation. Thanks to bguild. Anyway,bguild regex doesn't work because of the \w. The short hand includes digit numbers as well. In other words, it includes [A-Za-z0-9_] and the match, by default, is greedy and will attempt to match to the end, so it does not see the white space char but rather a period char. It won't back track the search so it returns not found.
/*
with ^BitTorrent\\s+\\d+\\s+\\w+\\s+
BitTorrent 6881 Server 0.01 Kbps 6963.48 Kbps
^-----------------------------------------------------^
Then there is no \\s+ because it found character . instead.
*/
"^Bit.+(\\d+\.\\d+)\s\\Kbps\\s$"
Did I add too many back slash? The 2 backslash before K aren't supposed to be there because there is no \K.
@tux4life, it is a good suggestion. I would go on that route too but the OP wants regex for a line, so I am not going to stop him/her.
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
@tux4life, it is a good suggestion. I would go on that route too but the OP wants regex for a line, so I am not going to stop him/her.
Perhaps you misunderstood the point I was trying to make.
radhakrishna.p said:
is it nessasary to use regular expression concept to get your actual output?
From what follows it seems like implying that split() doesn't make use of regular expressions - which it does - that's the point I wanted to make.
mvmalderen
Posting Maven
2,612 posts since Feb 2009
Reputation Points: 2,221
Solved Threads: 280
Skill Endorsements: 36
Question Answered as of 3 Months Ago by
mvmalderen,
Taywin,
bguild
and 1 other