REgular Expression using Java [duplicate] - java

This question already has answers here:
Java string split with "." (dot) [duplicate]
(4 answers)
Closed 8 years ago.
I am Using Regular Expression to break the string, I am trying to break the string but In reqular Expressions I am missing some format. Can any one please let me know where i went wrong.
String betweenstring="['Sheet 1$'].[DEPTNO] AS [DEPTNO]";
System.out.println("betweenstring: "+betweenstring);
Pattern pattern = Pattern.compile("\\w+[.]\\w+");
Matcher matchers=pattern.matcher(betweenstring);
while(matchers.find())
{
String filtereddata=matchers.group(0);
System.out.println("filtereddata: "+filtereddata);
}
I need to break like this:
['Sheet 1$']
[DEPTNO] AS [DEPTNO]

Given your very specific input, this regex works.
([\w\[\]' $]+)\.([\w\[\]' $]+)
Capture group one is before the period, capture group 2, after. To escape this for a Java string:
Pattern pattern = Pattern.compile("([\\w\\[\\]' $]+(\\.*[\\w\\[\\]' $]+)");
However, it would be much easier to split the string on the literal dot, if this is what you are trying to achieve:
String[] pieces = between.split("\\.");
System.out.println(pieces[0]);
System.out.println(pieces[1]);
Output:
['Sheet 1$']
[DEPTNO] AS [DEPTNO]

Related

Java regex for consecutive exponential terms [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Regex to first occurrence only? [duplicate]
(4 answers)
Closed 4 years ago.
I've tested this regex:
String regex = "[e][#](.)+[$]
the regex works well to identify exponential terms, however it breaks when there are two or more consecutive exponential terms.
I test the regex with the usual code:
while(matcher.find()){
String string = matcher.group();
System.out.println("this one: "+string);
}
When I type the expression:
e#x$ + 3e#x+1$
string equals to (e#(x$+3e#x+1$))
By the way, I added the parentheses inside the while loop. They are necessary for
what I am trying to accomplish.
I want the result of string to be (e(#x$))+3(e(#x+1$)
I know the problem lies in "(.)+ What I think is happening is that the regex
includes the first $, what I need is for it to stop at the first $.
How can I include this logic inside the regex?
thank you

java regular expression find and replace multiple [duplicate]

This question already has answers here:
Regular Expression for matching parentheses
(5 answers)
Closed 6 years ago.
I am trying to replace following code using regex in my code base.
if(StringFunctions.isNullOrEmpty(employee.getName())){
//java code
}
New code should be:
If(StringUtils.isEmpty(StringUtils.trim(employee.getName()))){
//java code
}
I have written following code to perform the update.
String regEx = "StringFunctions.isNullOrEmpty(.*)";
String replacement = "StringUtils.isEmpty(StringUtils.trim$1)";
textFromFile.replaceAll(regEx,pattern);
output is:
If(StringUtils.isEmpty(StringUtils.trim(employee.getName())){)
//java code
}
what is wrong in my code??? please help me
In regex () is the capturing group. Your regex pattern is incorrect because where you meant to put literal brackets, you have instead only put a capturing group.
The correct regex pattern is:
"StringFunctions.isNullOrEmpty\\((.+)\\)"
\\((.+)\\) means match a literal open bracket followed by (and capture) 1 or more of any character, followed by a literal closing bracket.
Testing:
String textFromFile = "if(StringFunctions.isNullOrEmpty(employee.getName())){}";
String regEx = "StringFunctions.isNullOrEmpty\\((.+)\\)";
String replacement = "StringUtils.isEmpty(StringUtils.trim($1))";
String output = textFromFile.replaceAll(regEx,replacement);
System.out.println(output);
Input:
if(StringFunctions.isNullOrEmpty(employee.getName())){}
Output:
if(StringUtils.isEmpty(StringUtils.trim(employee.getName()))){}

using if statment with regular expressions to know if String contains specified characters or not in java [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 7 years ago.
i have made this code and i don't know what is the problem with it, why the output print " f "??? even that the string contains the specified characters in the regex
String s="x^2+x-20";
Pattern pattern =
Pattern.compile("([+-][0-9]*)(([a-z A-Z])\\^2)"); //regex
Matcher matcher = pattern.matcher(s);
if(matcher.matches()){
System.out.println("t");
} else {
System.out.println("f");}
Your regex makes [+-] not an optional match but rather required. Use the following:
"([+-]?[0-9]*)(([a-z A-Z])\\^2)" // java syntax
Or as a regex without any java escaping:
([+-]?[0-9]?)(([a-z A-Z])\^2)
Also use Matcher.find rather than Matcher.match to find matches that are not the entire string.

Extract the data Using regular Expression in java [duplicate]

This question already has answers here:
Java string split with "." (dot) [duplicate]
(4 answers)
Closed 7 years ago.
I have the unproper data in this way. i need to extract the data before dot and after dot symbol using regular expression. I am using but i am not able to get exact data. please help. It is very urgent
Code:
Matcher matcher = Pattern.compile("([\\w[\\$##\\-^&]\\w\\[\\]' $]+)\\.([\\w\\[\\]' $]+)").matcher(formulaData);
while (matcher.find())
{
String Data=matcher.group(0);
String[] pieces = Data.split("\\.");
Heading=pieces[0].replace("\"", "");
Heading=pieces[1].replace("\"", "");
}//while
You can split by newline and then split by dot

Java regex error Illegal repetition [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Flex RegExp to Java RegExp
I don't know why is it not working.. I'm using java..
...
String patternString = "([^{}]*{[^{}]+}[^{}])*";
Pattern p = Pattern.compile(patternString);
...
The error that i receive is:
Illegal repetition near index 4
([^{}]*{[^{}]+}[^{}]*)
You need to escape the literal braces unless they're inside a character class:
String patternString = "([^{}]*\\{[^{}]+\\}[^{}])*";
Most other regex flavors can recognize when braces are not being used as a repetition operator (as in [0-9]{1,3}) and therefore will parse the regex correctly. But Java is insistent on having these braces escaped.

Categories

Resources