Results 1 to 7 of 7
Thread: Regex help?
- 01-18-2011, 05:12 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Regex help?
Well I'm writing this yet again because it signed me out when I went to post it, but anyhow, I am new to Java and am trying to use regex to extract an IP from a header but seems the more I read the more confused I'm getting. If someone could take a moment to post an example of how to extract a IP (127.0.0.1 for example) from a string and than print it so I could look it over and get a better understanding I would be very thankful. I've spent a fair amount of time reading various regex pages with no luck. I would really appreciate it. It's not the regex itself that confuses me it's actually implementing it to do this.
-
Let's see what you've tried so far, and please let us know how it doesn't work for you. Also, have you Googled "Java regular expression validating ip address"? Looks promising.
Last edited by Fubarable; 01-18-2011 at 05:16 AM.
-
- 01-18-2011, 05:22 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
I've tried a bunch of different thigns, I have also checked those sites. I'm still confused on how to actually use the regex for this purpose. I don't see why it's so hard to just post an example of how to take the 127.0.0.1 out of a string like "Hey this is my 127.0.0.1", I've spent alot of time reading various sites and still am confused.
Also I checked out that site you posted last night, which just further confused me. I'm sure there's got to be a simpler example that doesn't have all kinds of extra code in it.Last edited by l3ane; 01-18-2011 at 05:32 AM.
- 01-18-2011, 05:39 AM #5
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
here's an example. note, this will not check whether your IP is really in a valid Class.
Java Code:public class RegexDemo2 { private static Pattern ex = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"); private static Matcher match; public static void main(String[] args){ String str = "this is my ip 127.0.0.1 the end. but here is another 10.10.1.0 "; match = ex.matcher(str); while (match.find() ){ System.out.println( match.group() ); } } }
- 01-18-2011, 05:48 AM #6
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
first you have to define what the format for a correct IP address
for example 192.168.0.198, 198.234.89.0,.......
i don't know the rule regarding it but i think it consists of four parts separated by "." sign. if the numbers are of three digits then we can make regex as follows
"([\\d]){1,3}\\.([\\d]){1,3}\\.([\\d]){1,3}\\.([\\d]){1,3}"
i think you can change it as actual rule regarding IP address.
- 01-18-2011, 06:04 AM #7
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Using Regex
By ron87 in forum New To JavaReplies: 4Last Post: 09-12-2010, 06:26 AM -
Regex problem
By Nimyz in forum Advanced JavaReplies: 4Last Post: 05-14-2010, 07:17 AM -
Please Help - Regex
By BeeGee in forum Advanced JavaReplies: 0Last Post: 04-28-2010, 05:28 PM -
Help with regex
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 03-29-2010, 12:40 AM -
Using regex
By SteroidalPsycho in forum New To JavaReplies: 0Last Post: 03-28-2010, 11:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks