Validate IP Address With Java Regular Expression
Jan 23, 2015
I am trying to implement an example (Book* : Java SE 7 ..By S G Ganesh) for validating an IP address but it fails to validate a valid IP addresses. I found another example on the internet(**) and it works super fine, no problem at all. I edited the code (the one I got from internet) into the exact format like book and it still works super but i don't understand why the books' example doesn't work though both look exactly the same now ,further more, how can i compare String x and y for equality?
public class TestClass {
void validateIP(String ipStr) {
String one = "((25[0-5]|2[0-4]d|[01]?dd?)(.)){3}(25[0-5]|2[0-4]d|[01]?dd?)"; //copied from internet and edited
String two = "((25[0–5]|2[0–4]d|[01]?dd?)(.)){3}(25[0–5]|2[0–4]d|[01]?dd?)"; // copied from book
String x = "((25[0-5]|2[0-4]d|[01]?dd?)(.))";
String y ="((25[0–5]|2[0–4]d|[01]?dd?)(.))";
[Code] ....
View Replies
ADVERTISEMENT
Apr 17, 2014
I have written below regex for two lines.
String LN1Pattern = "^((?=.{1,35}$)(/([A-Z]{1,5})(|(//[a-zA-Z0-9]+))))$";
System.out.println("/ABC//FGhiJkl012345".matches(LN1Pattern));
String LN2Pattern = "^(|((s+(//[a-zA-Z0-9]{1,33})){1,2}))$";
System.out.println("".matches(LN2Pattern));
s+ is a newline character.
But when I combines both as below, its not giving me expected result.
^(((?=.{1,35}$)(/([A-Z]{1,5})(|(//[a-zA-Z0-9]+))))(|((s+(//[a-zA-Z0-9]{1,33})){1,2})))$
For string "/ABC//FGhiJkl012345
//abCD01EF02" - returns False. Expected is True
I think there is some problem in lookahead placed.
View Replies
View Related
Nov 28, 2013
I need a regular expression in java for phone number which that does not allow any character special character only numbers should be allowed.. My sample program is
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class c {
public String removeOrReplacePhoneNumber(String input) {
if (null == input)
return input;
[Code] ....
but this is allowing characters like a,b,c..
View Replies
View Related
Feb 18, 2014
I need to parse an html web page to extract specific information from the tags in Java. For example,
<b>Species </b> Strain </td>
I need to look for the Strain info (Strain is variable in length) in the page. The whole web page is stored as a huge string. I need a regular expression that can identify all the Species and retrieve their corresponding strain info. how to do this or can propose some clever string manipulation methods in Java.
View Replies
View Related
Mar 7, 2014
Do not want to use loop and Character.isLetter to validation string , not sure at how to using regular expression?
If I want 8 characters string length, the first 3 is letter, the remind character is number ...
^[a-z0-9_-]{8}$
^[a-zA-Z]{3} + [/d]{5} $ ??
View Replies
View Related
Jul 12, 2014
I have a text file that has the following lines:
the boy is smart
He is from Australia
** Highly important line
That's all
Now, I need a regular java expression that would match a line in this file that begins with the ** characters. That is, when I match the text with the regex, I should get only this line :
** Highly important line
How would I write this regex in java?
View Replies
View Related
Aug 25, 2014
I have to match pattern like 76XYYXXXX mean x can be 4or 5 and Y can be 6 or 7. All x and y should be same .i.e. 764664444
View Replies
View Related
Jan 17, 2014
I am currently enrolled in my first Java class. I have taken C# in the past so I've messed with some regular expressions, but I am having trouble finding a good website for Java. Is the syntax the same?I want to create a regular expression to only allow the following characters: c C f F [and any 1-3 digit number].
View Replies
View Related
Mar 16, 2015
I have a string "We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle" I need to replace the numbers based on the below condition.
if more then 5, replace with many
if less then 5, replace with a few
if it is 1, replace with "only one"
below is my code, I am missing the equating part to replace the numbers
private static String REGEX="(d+)";
private static String INPUT="We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle";
//String pattern= "(.*)(d+)(.*)";
private static String REPLACE = "replace with many";
public static void main(String[] args) {
// Create a Pattern object
[Code]...
View Replies
View Related
Feb 4, 2015
I have some problems about performing regular expression. In the following code, the output as a result is being "valid last name". But in my opinion, it should be "invalid last name". Because "Doe" has not any space, apostroph or any hypen. Look at the regular expression statement: "[a-zA-Z]+([ '-][a-zA-Z]+)*"
package deneme;
public class Deneme {
public static void main(String[] args) {
String lastName = "Mc-Something"; // Mc-Something
if(!lastName.matches("[a-zA-Z]+([ '-][a-zA-Z]+)*"))
System.out.println("Invalid last name");
else
System.out.println("Valid last name");
}
}
I know that the asterisk qualifier means character(s) in any number.(Is this wrong?) So for "Mc-Something", 'M' corresponds to [a-zA-Z], 'c' corresponds to +, - corresponds to [ '-], 'S' corresponds to [a-zA-Z], "o" corresponds to +, and finally "mething" corresponds to *, doesn't they? But for "Doe", there is no any space, apostroph or hypen.
View Replies
View Related
Dec 1, 2014
I am new to java coding. so, reading the log file using java code.
1) I wish to parse a file, and find a value for variable e.g. [StorageVersion].
2) After finding the value, it will be pushed to database.
Following is the code that i have written till now.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.util.regex.*;
[code]....
1) I don't know what regular expression can i use.
2) I want a line to check that preceding line should have //StorageVersion .
3) n then i will check any string between := K[' and '];. From above log file "'706146.0.22106932'" is the value that i am looking for.
View Replies
View Related
Aug 6, 2014
I have to read a text looking for value related to certain variable. The variable are always DE plus identifier from 1 to 99 or DE plus identifier from 1 to 99 and SF plus identifier from 1 to 99. The value can be alphanumeric. How can I get these values? As a start point, I try to use split("DE") but I can get something that is wrong if there is "DE" inside the text that doesn't me interest. I look for scanner but it doesn't work. I guess that there is some way, maybe using regex but I am completely lost ( I have never used regedix before and I am in rush to fix this).
Basically, the text is similar to the below where DE means data element and sf sub field. Some data elements have sub fields while others don't. I guess that there is a way to split with something like DE+anyNumberFrom1To99 = theValueAimed in some array and DE+anyNumberFrom1To99+,+SF+anyNumberFrom1To99 = theValueAimed in other array.
DE 2, SF 1 = 00 SOME TEXT THAT DOESN'T INTEREST ME
DE 22, SF 1 = 0 SOME TEXT THAT DOESN'T INTEREST ME
DE 22, SF 4 = 1 SOME TEXT THAT DOESN'T INTEREST ME
DE 22, SF 5 = 0 SOME TEXT THAT DOESN'T INTEREST ME
DE 22, SF 6 = 11 SOME TEXT THAT DOESN'T INTEREST ME
DE 22, SF 7 = 90x SOME TEXT THAT DOESN'T INTEREST ME
DE 22, SF 7 = 12ab SOME TEXT THAT DOESN'T INTEREST ME
DE 99 = 1234 SOME TEXT THAT DOESN'T INTEREST ME
View Replies
View Related
Mar 3, 2014
Regular expression which I want to use to split a string. The string could look similar to this:
"a = "Hello World" b = "Some other String" c = "test""
The String is read from a file where the file contents would be:
a = "Hello World" b = "Some other String" c = "test"
After splitting I would like to get the following array:
String[] splitString = new String[] {"a", "=", ""Hello World"", "b", "=", ""Some other String"", "c", "=", ""test""}
I know I could just write a simple text parser to go over the string character by character, but I would rather not do it to keep my code clean and simple. No need to reinvent the wheel.
However, I just cant seem to be able to find the right regular expression for this task. I know that a RE must exist because this can be solved by a finite automaton.
View Replies
View Related
Jan 1, 2015
I need a pattern that matches Hello Smith (STL Terminal) (15.0).
View Replies
View Related
Sep 4, 2014
I have a header in file like below:
EMP_ID|EMP_NAME|DEPARTMENT|SALARY|ACTIVE1
passed to a string
String test = "EMP_ID|EMP_NAME|DEPARTMENT|SALARY|ACTIVE1";
I have to check if the header has only alphanumeric and pipedelimiter is allowed.
Other than these i need to raise an error.
View Replies
View Related
Nov 12, 2014
I want to ask how to track ip address and mac address of a different computer using java?
View Replies
View Related
May 14, 2014
I'm guessing the vehicle checklist is for making your car? So like for the tires there would be 3 options and you check which one you want?
View Replies
View Related
May 18, 2014
How to read the excel sheet data and validate that null values are inserted or columns data exceeded with respected to header.and these validated data should be stored into 3 different tables... My requirement is college data excel sheet with combination of all groups like mba,mca,and etc...then re ad the data and find the groups then
1. give entry in the UP_LOAD_DATA_HIST table with this excel data uploading with this user and this date,
2. find the group of the department and save data into CATAGIRY_TBL.
3. Finally save the All data into the students table.
View Replies
View Related
Jan 12, 2014
I am designing an address book. I have come up with the following design:
Address Book:
Attributes : Entry
Methods: add(), delete(), search()
Entry:
Attributes : Name, Address, Phone Number
Methods: addName(),editName(),deleteName(),addPhone(),editPhone(),deletePhone(),addAddress(),editAddress(),deleteAddress()
Name:
Attributes: firstName, lastName
PhoneNumber:
Attributes:areaCode, phoneNumber
Address:
Attributes:streetNumber,streetName,city,zip.
View Replies
View Related
Oct 8, 2014
i found a code to get emails from gmail in this forum.It's working very well, now i want to delete a specific sender email address.
So i can have the sender email with this:
Java Code: String senderAddress = aMessage.getFrom()[0].toString();
System.out.println(" From: " + senderAddress); mh_sh_highlight_all('java');
Now i want to do something like
Java Code: If (senderAddress = "test@gmail.com"){
message.setFlag(Flags.Flag.DELETED, true);
} mh_sh_highlight_all('java');
but it's not working.
View Replies
View Related
Mar 17, 2015
I wanted to ask is there any way that Java code can get at the address of some object or other datum? I've found this useful a few time with certain high performance C# libraries that I've developed.
Also does Java have the equivalent of C#'s "fixed" buffers? which are inline contiguous arrays of primitives (like Chars or Ints etc).
View Replies
View Related
Feb 2, 2015
I am doing an assignment for a college class. We are asked to get user input and decide if it is a valid IP address and then check what class the address is and if it is a public or private address.
So far, I can get the input, and check to see if the numbers are in a valid range. I can also display the IP address to the user. I am having an issue figuring out how to get the program to check the classes and whether they are public or private.
import javax.swing.JOptionPane;
/**
This program will take user input and calculate whether it is a valid IP address and the class that it belongs to.
*/
public class shortONE_1
{
public static void main(String[] args)
{
//Variables
final int MIN_OC = 1; //Minimum number accepted
final int MAX_OC = 255; //Maximum number accepted
final int MIN_A = 1; //Min number for class A
final int MAX_A = 127; //Max number for class A
[Code] ....
View Replies
View Related
Jul 17, 2013
So far I only know using
request.getRemoteAddr() get
to get user IP address, is that a way to get Users PC memory?
long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
Only get server memory not the client....
View Replies
View Related
May 21, 2014
i have file index.jsp , that approach to reguar java class and call function . in that function i want to get attribute from session like this :
List<Coupon> couponsList = (List<Coupon>)request.getSession().getAttribute("listOfCouponsThatNotExpired");
because this regular java class i get error message ,how to solve this ?
View Replies
View Related
Mar 1, 2014
I want to do a simple search and replace regular expression of lines. I am very unfamilar with Java regular expressions, and I'm not sure how to do something as simple as what I want to do. I have lines that look like this...
Java Code : access_log /home/%USER%/access_log mh_sh_highlight_all('java');
I want them changed so %USER% changes to a string, such as "cyrus," so they appear like this ...
Java Code: access_log /home/cyrus/access_log mh_sh_highlight_all('java');
The reason I want to use regular expressions is because I want to use the replaceAll method of the java.lang.String object. If I use replace I have to convert my strings into char arrays, and my code becomes bulky.
View Replies
View Related
May 5, 2014
In this i Write a Java program to parse a syntactically correct arithmetical expression and produce an equivalent Expression TREE. Remember, in an expression tree the terminal nodes are variables and constants, and the interior nodes are operators (such as +,-,*,/). For instance the expression: (1 + 2) * 3 can be represented by the following tree:
INPUT Expression
(1 +2)*3
POSTORDER Expression
1 2 + 3 *
TREE [root=asd.reg.Node@56ddc58]
But when I Execute the program it Shows only Prefix and postfix .But the requered output is in inorder preorder and postorder ...
import ava.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.tree.TreeNode;
[code]....
View Replies
View Related