Reputation: 542
I have a text like this:
ASD.123.av.234.X_975.dfgdfg
and i wish to round it up to:
.123.234.
"\\.[0-9]+\\."
this represents my current pattern to select the .number. but negative lookahead didnt work for me: "(?!^\\.[0-9]+\\.)"
do you guys have any ideea how to achieve this?
Upvotes: 0
Views: 109
Reputation: 46841
Simply try with Lookaround to select the .number.
The lookaround actually matches characters, but then gives up the match, returning only the result: match or no match
(?<=\.)\d+(?=\.)
Pattern explanation:
(?<= look behind to see if there is:
\. '.'
) end of look-behind
\d+ digits (0-9) (1 or more times)
(?= look ahead to see if there is:
\. '.'
) end of look-ahead
Sample code:
String str = "ASD.123.av.234.X_975.dfgdfg";
Pattern p = Pattern.compile("(?<=\\.)\\d+(?=\\.)");
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(m.group());
}
output:
123
234
What is meaning of your regex pattern?
(?! look ahead to see if there is not:
^ the beginning of the string
\. '.'
[0-9]+ any character of: '0' to '9' (1 or more times )
\. '.'
) end of look-ahead
The problem is with ^
that asserts the beginning of the string.
Upvotes: 1
Reputation: 1382
Try the regex below. It would get you all groups of .number.
. You would have to yoin the nymbers later though.
(\.[0-9]+\.)+
Try this site for further tests: http://www.regexplanet.com/advanced/java/index.html
Upvotes: 0
Reputation: 6420
Try this regex: (\.[0-9]+?\.)(?:.*?\.([0-9]+?\.))+
.
In your output above, you seem to don't want to have .123..234.
but .123.234.
This regex accomplishes that. It works without lookaheads.
Upvotes: 0
Reputation: 67968
(?=.*?\.\d+\..*?).*?(.\d+.).*?
You can try this.It employs positive lookahead.
http://regex101.com/r/dK7kR5/1
Upvotes: 0