I am trying to use Regex to extract the values from a string and use them for the further processing.
The string I have is :
String tring =Format_FRMT: <<<$gen>>>(((valu e))) <<<$gen>>>(((value 13231)))
<<<$gen>>>(((value 13231)))
Regex pattern I have made is :
Pattern p = Pattern.compile("\\<{3}\\$([\\w ]+)\\>{3}\\s?\\({3}([\\w ]+)\\){3}");
When I am running the whole program
Matcher m = p.matcher(tring);
String[] try1 = new String[m.groupCount()];
for(int i = 1 ; i<= m.groupCount();i++)
{
try1[i] = m.group(i);
//System.out.println("group - i" +try1[i]+"\n");
}
I am getting
No match found
Can anybody help me with this? where exactly this is going wrong?
My first aim is just to see whether I am able to get the values in the corresponding groups or not. and If that is working fine then I would like to use them for further processing.
Thanks
Here is an exaple of how to get all the values you need with find():
String tring = "CHARDATA_FRMT: <<<$gen>>>(((valu e))) <<<$gen>>>(((value 13231)))\n<<<$gen>>>(((value 13231)))";
Pattern p = Pattern.compile("<{3}\\$([\\w ]+)>{3}\\s?\\({3}([\\w ]+)\\){3}");
Matcher m = p.matcher(tring);
while (m.find()){
System.out.println("Gen: " + m.group(1) + ", and value: " + m.group(2));
}
See IDEONE demo
Note that you do not have to escape < and > in Java regex.
After you create the Matcher and before you reference its groups, you must call one of the methods that attempts the actual match, like find, matches, or lookingAt. For example:
Matcher m = p.matcher(tring);
if (!m.find()) return; // <---- Add something like this
String[] try1 = new String[m.groupCount()];
You should read the javadocs on the Matcher class to decide which of the above methods makes sense for your data and application. http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html
Related
I'm trying to design a regular expression to identify certain columns in the string. This is the input string -
GENDER = Y OR (SUM(TOTAL_AMOUNT) > 100 AND SUM(TOTAL_AMOUNT) < 600)
I'm trying to match SUM(TOTAL_AMOUNT) from above string.
This is the regex I've tried:
SUM([a-zA-Z])
But its not able to match properly. Could someone tell me what I'm doing wrong with my regex here. Thanks in advance.
Sample Code:
List<String> input = new ArrayList<>();
Matcher m = Pattern.compile("SUM([a-zA-Z])").matcher(str);
while (m.find())
input.add(m.group(1));
You can use
String str = "GENDER = Y OR (SUM(TOTAL_AMOUNT) > 100 AND SUM(TOTAL_AMOUNT) < 600)";
Matcher matcher = Pattern.compile("SUM\\([^()]+\\)").matcher(str);
List<String> input = new ArrayList<>();
while (matcher.find()) {
input.add(matcher.group());
}
System.out.println(input);
See the Java demo online. See the regex demo, too. It matches
SUM\( - a SUM( string
[^()]+ - one or more chars other than ( and )
\) - a ) char.
Note that I am using matcher.group() in the code to get the full match since there is no capturing group in the pattern (thus, you can't use matcher.group(1) here).
I'm trying to access a certain part of multiple strings that follow a pattern.
Here's an example of what I'm trying to do.
String s = "Hello my name is Joe";
if(Pattern.matches(s,"Hello my name is ([\\w]*)"))
{
System.out.println("Name entered: $1");
}
However, my code never enters inside the "if-statement"
Swap the parameters to the matches method, and your if will work (regex is the 1st parameter, not the second).
However, you still won't print the first capturing group with $1. To do so:
String s = "Hello my name is Joe";
Matcher m = Pattern.compile("Hello my name is ([\\w]*)").matcher(s);
if(m.matches())
{
System.out.println("Name entered: " + m.group(1));
}
I think that you are looking for this:
final String s = "Hello my name is Joe";
final Pattern p = Pattern.compile("Hello my name is (\\w++)");
final Matcher m = p.matcher(s);
if (m.matches()) {
System.out.printf("Name entered: %s\n", m.group(1));
}
This will capture the \w++ group value, only if p matches the entire content of the String. I've replaced \w* with \w++ to exclude zero length matches and eliminate backtracks.
For further reference take a look at The Java Tutorial > Essential Classes - Lesson: Regular Expressions.
Try using the Matcher class, see this page for more information about Regular Expressions in Java http://java.sun.com/developer/technicalArticles/releases/1.4regex/
Because you have the parameters to Pattern.matches() backwards.
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
Also, you don't need a set (the brackets). In addition, You are going to need to use the Matcher class and get the brackreference via the group() method.
I have a FileInputStream who reads a file which somewhere contains a string subset looking like:
...
OperatorSpecific(XXX)
{
Customer(someContent)
SaveImage()
{
...
I would like to identify the Customer(someContent) part of the string and switch the someContent inside the parenthesis for something else.
someContent will be a dynamic parameter and will contain a string of maybe 5-10 chars.
I have used regEx before, like once or twice, but I feel that in a context such as this where I don't know what value will be inside the parenthesis I'm at a loss of how I should express it...
In summary I want to have a string returned to me which has my someContent value inside the Customer-parenthesis.
Does anyone have any bright ideas of how to get this done?
Try this one (double the escaping backslashes for the use in java!)
(?<=Customer\()[^\)]*
And replace with your content.
See it here at Regexr
(?<=Customer\() is look behind assertion. It checks at every position if there is a "Customer(" on the left, if yes it matches on the right all characters that are not a ")" with the [^\)]*, this is then the part that will be replaced.
Some working java code
Pattern p = Pattern.compile("(?<=Customer\\()[^\\)]*");
String original = "Customer(someContent)";
String Replacement = "NewContent";
Matcher m = p.matcher(original);
String result = m.replaceAll(Replacement);
System.out.println(result);
This will print
Customer(NewContent)
Using groups works and non-greedy works:
String s =
"OperatorSpecific(XXX)\n {\n" +
" Customer(someContent)\n" +
" SaveImage() {";
Pattern p = Pattern.compile("Customer\\((.*?)\\)");
Matcher matcher = p.matcher(s);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
will print
someContent
Untested, but something like the following should work:
Pattern pattern = Pattern.compile("\\s+Customer\\(\\s*(\\w+)\\s*\\)\\s*");
Matcher matcher = pattern.matcher(input);
matcher.matches();
System.out.println(matcher.group(1));
EDIT
This of course won't work with all possible cases:
// legal variable names
Customer(_someContent)
Customer($some_Content)
I'm trying to parse some text, but for some strange reason, Java regex doesn't work. For example, I've tried:
Pattern p = Pattern.compile("[A-Z][0-9]*,[0-9]*");
Matcher m = p.matcher("H3,4");
and it simply gives No match found exception, when I try to get the numbers m.group(1) and m.group(2). Am I missing something about how Java regex works?
Yes.
You must actually call matches() or find() on the matcher first.
Your regex must actually contain capturing groups
Example:
Pattern p = Pattern.compile("[A-Z](\\d*),(\\d*)");
matcher m = p.matcher("H3,4");
if (m.matches()) {
// use m.group(1), m.group(2) here
}
You also need the parenthesis to specify what is part of each group. I changed the leading part to be anything that's not a digit, 0 or more times. What's in each group is 1 or more digits. So, not * but + instead.
Pattern p = Pattern.compile("[^0-9]*([0-9]+),([0-9]+)");
Matcher m = p.matcher("H3,4");
if (m.matches())
{
String g1 = m.group(1);
String g2 = m.group(2);
}
I'm new to using patterns and looked everywhere on the internet for an explanation to this problem.
Say I have a string: String info = "Data I need to extract is 'here' and 'also here'";
How would I extract the words:
here
also here
without the single quotes using a pattern?
This is what I have so far...
Pattern p = Pattern.compile("(?<=\').*(?=\')");
But it returns ( here and 'also here ) minus the brackets, that is just for viewing. It skips over the second piece of data and goes straight to the last quote...
Thank you!
EDIT:
Thank you for your replies everyone! How would it be possible to alter the pattern so that here is stored in matcher.group(1) and also here is stored in matcher.group(2)? I need these values for different reasons, and splitting them from 1 group seems inefficient...
Try making your regex non-greedy:
Pattern p = Pattern.compile("(?<=')(.*?)(?=')");
EDIT:
This does not work. It gives the following matches:
here
and
also here
This is because the lookahead/lookbehind do not consume the '.
To fix this use the regex:
Pattern p = Pattern.compile("'(.*?)'");
or even better (& faster):
Pattern p = Pattern.compile("'([^']*)'");
I think you're making it to complicated, try
Pattern.compile("'([^']+)'");
or
Pattern.compile("'(.*?)'");
They will both work. Then you can extract the result from the first group matcher.group(1) after performing a matcher.find().
This should work for you:
Pattern p = Pattern.compile("'([\\w\\s]+)'");
String info = "Data I need to extract is 'here' and 'also here'";
Matcher m = p.matcher(info);
while (m.find()) {
System.out.println(m.group(1));
}
Here's the printout:-
here
also here
If you want the data in 2 separate groups, you could do something like this:-
Pattern p = Pattern.compile("^[\\w\\s]*?'([\\w\\s]+)'[\\w\\s]*?'([\\w\\s]+)'$");
String info = "Data I need to extract is 'here' and 'also here'";
Matcher m = p.matcher(info);
while (m.find()) {
System.out.println("Group 1: " + m.group(1));
System.out.println("Group 2: " + m.group(2));
}
Here's the printout:
Group 1: here
Group 2: also here
Why not using simply the following?
'.*?'