Regular Expression Match For String Containing Multiple Braces
Jan 1, 2015I need a pattern that matches Hello Smith (STL Terminal) (15.0).
View RepliesI need a pattern that matches Hello Smith (STL Terminal) (15.0).
View RepliesI 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.
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.
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} $ ??
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 RelatedI 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 RelatedI 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]...
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.
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..
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.
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
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.
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] ....
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.
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?
Why the following string fails the test below:
@Pattern(regexp = "^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(.[a-zA-Z0-9-]+)*.(([0-9]{1,3})|" +
"([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$", message = "Not a well-formed email address")
I'm trying to insert some data of type date into database table using hibernate.i take the input date from an xhtml form as shown below
addEvent.xhtml
Event Date (dd-mon-yy) :
<br/>
<h:inputText id="eventdate" value="#{eventBean.eventDate}" p:required="required"
p:type="date"/>
<p/>
<h:commandButton value="Add Event" actionListener="#{eventBean.addEvent}" />
<p/>
This is my addEvent method in EventBean.java
public void addEvent(ActionEvent evt) {
uname = Util.getUname();
boolean a = EventDAO.add(this);
if ( a) {
message = "Event has been added!";
[Code] ....
While executing this..i get the following error: ORA-01861: literal does not match format string. Could it be due to any mismatch in date format (chrome browser automatically takes date in the format mm-dd-yyyy )? If yes, how do I resolve it? (I'm using Oracle database)
I have a table which contains list of regular expression and its corresponding value.I have to fetch those value and put it HASHMAP where regex as key.I have to compare the each key with the given string(input) and If matches I have to get the corresponding Value for the regex.
View Replies View RelatedI have 2 strings
String value ="/abc_12_1/abc234/abc/filename.txt";
String src="abc"
Sring tgt=xyz
Now I have to replace the string in variable value in case of exact match; I am trying to do the following :
if(value.contains(src)
{
value.replaceall(src,tgt);
}
Now the problem here is it replaces all the occurrence of abc in the string value and I get the below output as :
value=""/xyz_12_1/xyz234/xyz/filename.txt";
However my requirement is only in the case the value exactly matches with source the replacement shd happen. I am expecting the output like this :
String value ="/abc_12_1/abc234/xyz/filename.txt";
Also the above code is in a function which will be called multiple times and the values will keep on changing. However the target and source will remain the same always.
I am trying to match the input string with range of values and getting PatternSyntaxException. below is my code
public static void main(String[] args) {
String regex="01[4-6]";
String code="015";
boolean bool=code.matches(regex);
System.out.println(bool);
}
output: true
But when i try to give range in double digits, it throws exception. What if i have to match a number(as string) between 10 to 25 or like that.
public static void main(String[] args) {
String regex="0[14-16]";
String code="015";
boolean bool=code.matches(regex);
System.out.println(bool);
}
Output:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal character range near index 5
0[14-16]
^
I have a large text file of 1 GB size. I need to print the line when a matching word is found in a particular line. Below is the code I am using. But if there are many lines that has the matching word, it's taking lot of time. Any solution to print the lines much faster.
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.contains("xyz")) {
System.out.println(line);
}
}
In switch case statement string expression whether it will work or not ???
View Replies View RelatedI'm doing some revision for my OCAJ atm, & I came across this code in a mock question which takes two int arguments & simply returns the larger.
public int max(int x, int y){
if (x > y)
return x;
return y;
}
When evaluating it I thought this would be invalid code as would always return x. But it transpires I was way off & that this actually works! In playing around with it, it seems like the first return statement is treated as:
if {// this bit } & the second return is treated as: else{ //this bit}.
What baffles me though is that you can put any amount of additional statements before the second return and it continues to work, however if you put even a single statement before the first return, the whole thing falls over.
I guess my two questions are - Am I right in my discovery above ( that statements preceeding the first return will always break it)? & secondly is this a good way of coding? for readability, I would always do it as:
public int max(int x, int y){
if (x > y){
return x;
}
else{
return y;
}
}
I have read some on this but I'm trying to understand a code and the following part confuses me
if( thisCount == bestCount )
bestCandidates.add( candidate );
else if( thisCount > bestCount ) {
bestCount = thisCount;
bestCandidates.clear();
bestCandidates.add( candidate );
}
}
What I find confusing is this: If I am not mistaken the need for curly braces occurs when using more than one statement and if you use else statements.
So I wonder if I have missunderstood about the else statement and that you dont need curly braces around a one statement if statement that is followed by an else statement...
i created a class and a constructor for the class. then i used getters and setters.in the setters i'm trying to write a line that will be -
if (num < 0|| num>120) {
dont change value and do nothing
}
else{
num1 = num
}
how can i do this ? i tried to put an empty curly braces but it gives me an error.
Here if we give input from a text file from a disk and (a[b+c{d}]-v) is true and{a-(b]} is false but i get some syntax errors ...
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Stack;
public class Syste {
private static String expr;
[code]....