How do extract values from a regex where any placeholder can be rederenced by a $number_of_occurance value?
For example, I have a string final_0.25Seg1-Frag1 and I want to find all matches of this string in a file with 0.25 as a wildcard, which I can do using
Pattern regex = Pattern.compile( "/vod/final_\\d+\\.\\d+Seg1-Frag1" );
Matcher regexMatcher = regex.matcher(data2[0]);
I want to retain the value of the value in \\d+\\.\\d and find which among all the matched lines has the biggest value in this position.
Have you looked at Pattern groups ? You can iterate through these to identify matched subexpressions.
From the linked example. Matcher.group(0) is the complete expression.
CharSequence inputStr = "abbabcd"; // could be a String type
String patternStr = "(a(b*))+(c*)";
// Compile and use regular expression
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find();
if (matchFound) {
// Get all groups for this match
for (int i=0; i<=matcher.groupCount(); i++) {
String groupStr = matcher.group(i);
}
}
Please give an example. I guess you need to read Mathcer docs http://docs.oracle.com/javase/7/docs/api/index.html?java/util/regex/Matcher.html. You can access capturing groups via group method.
Related
I am very new to Regex.
I have String from which i tried fetching value.
String conditionExpression= "{action==\"Submit\" && orgType== \"supply\"}";
Matcher matcher = Pattern.compile("(?<=orgType==)\"[^\"]+\"").matcher(conditionExpression);
if (matcher.find()) {
orgType = matcher.group().replaceAll("\"", "");
}
Input will be String : "{action=="Submit" && orgType== "supply"}"
Output will be value of orgType: supply
Tried fetching orgType using Regex , but its returning null. Anything wrong here?
You need to account for whitespace that may appear around the equals sign. Besides, there is no need to post-process the match value if you use a capturing group around [^"]+.
Here is a fixed code:
String orgType = "";
String conditionExpression= "{action==\"Submit\" && orgType== \"supply\"}";
Matcher matcher = Pattern.compile("orgType\\s*==\\s*\"([^\"]*)\"").matcher(conditionExpression);
if (matcher.find()) {
orgType = matcher.group(1);
}
System.out.println(orgType); // => supply
See the Java demo
The \\s*==\\s* part of the pattern matches == enclosed with 0+ whitespace chars.
The ([^\"]*) pattern is a capturing group that pushes a submatch value into Group 1 that you can retrieve via matcher.group(1) (no need to remove double quotes later).
I am new into java regex and I could't find an answer.
This is my regex: -?\\d*\\.?\\d+(?!i)
and I want it not to recognize eg. String 551i
This is my method:
private static double regexMatcher(String s, String regex) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s.replaceAll("\\s+", ""));
if (!matcher.find()) {
return 0;
}
String found = matcher.group();
return Double.parseDouble(matcher.group());
}
I want this method to return 0.0 but it keeps returning 55.0.
What am I doing wrong?
Use an atomic group to avoid backtracking into the whole digit dot digit matching pattern:
"-?(?>\\d*\\.?\\d+)(?!i)"
See the Java demo and a regex demo.
i have the following text:
bla [string1] bli [string2]
I like to match string1 and string2 with regex in a loop in java.
Howto do ?
my code so far, which only matches the first string1, but not also string 2.
String sRegex="(?<=\\[).*?(?=\\])";
Pattern p = Pattern.compile(sRegex); // create the pattern only once,
Matcher m = p.matcher(sFormula);
if (m.find())
{
String sString1 = m.group(0);
String sString2 = m.group(1); // << no match
}
Your regex is not using any captured groups hence this call with throw exceptions:
m.group(1);
You can use just use:
String sRegex="(?<=\\[)[^]]*(?=\\])";
Pattern p = Pattern.compile(sRegex); // create the pattern only once,
Matcher m = p.matcher(sFormula);
while (m.find()) {
System.out.println( m.group() );
}
Also if should be replaced by while to match multiple times to return all matches.
Your approach is confused. Either write your regex so that it matches two [....] sequences in the one pattern, or call find multiple times. Your current attempt has a regex that "finds" just one [...] sequence.
Try something like this:
Pattern p = Pattern.compile("\\[([^\\]]+)]");
Matcher m = p.matcher(formula);
if (m.find()) {
String string1 = m.group(0);
if (m.find(m.end()) {
String string2 = m.group(0);
}
}
Or generalize using a loop and an array of String for the extracted strings.
(You don't need any fancy look-behind patterns in this case. And ugly "hungarian notation" is frowned in Java, so get out of the habit of using it.)
I'm trying to do some pattern matching in Java:
Pattern p = Pattern.compile("(\\d+) (\\.+)");
Matcher m = p.matcher("5 soy milk");
String qty = m.group(1);
String name = m.group(2);
I want to end up with one string that contains "5" and one string that contains "soy milk". However, this pattern matching code gives me an IllegalStateException.
You have to call matches() before you attempt to get the groups.
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#matches()
public boolean matches()
Attempts to match the entire region against the pattern.
If the match succeeds then more information can be obtained via the start, end, and group methods.
Try this:
Pattern p = Pattern.compile("(\\d+) (\\.+)");
Matcher m = p.matcher("5 soy milk");
if (m.matches())
{
String qty = m.group(1);
String name = m.group(2);
}
This is because you don't initiate your Matcher. You should p.matcher(...).matches() (or .find(), or .lookingAt(), depending on the desired behaviour -- real regex matching is done with .find()).
And check the result of .matches() since in your case it returns false: \.+ ("\\.+" in a Java string) will try and match a dot one or more times; you should use .+ (".+" in a Java string) to match "any character, one or more times".
If I have a regular expression, how do I return the substring that it has found?
I'm sure I must be missing something obvious, but I've found various methods to confirm that that substring is contained in the string I'm searching, or to replace it with something else, but not to return what I've found.
Matcher matcher = Pattern.compile("a+").matcher("bbbbaaaaabbbb");
if(matcher.find())
System.out.println(matcher.group(0)); //aaaaa
If you want specific parts
Matcher matcher = Pattern.compile("(a+)b*(c+)").matcher("bbbbaaaaabbbbccccbbb");
if(matcher.find()){
System.out.println(matcher.group(1)); //aaaaa
System.out.println(matcher.group(2)); //cccc
System.out.println(matcher.group(0)); //aaaaabbbbcccc
}
Group 0 is the complete pattern.. other groups are separated with parenthesis in the regex (a+)b*(c+) and can be get individually
CharSequence inputStr = "abbabcd";
String patternStr = "(a(b*))+(c*)";
// Compile and use regular expression
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find();
if (matchFound)
{
// Get all groups for this match
for (int i=0; i<=matcher.groupCount(); i++)
{
String groupStr = matcher.group(i);
}
}
A CharSequence is a readable sequence of char values. This interface provides uniform, read-only access to many different kinds of char sequences. A char value represents a character in the Basic Multilingual Plane (BMP) or a surrogate. Refer to Unicode Character Representation for details.
CharSequence is an interface
public interface CharSequence
See Capturing groups
See group with parameter example
See Java Regex Tutorial
import java.util.regex.*;
class Reg
{
public static void main(String [] args)
{
Pattern p = Pattern.compile("ab");
Matcher m = p.matcher("abcabd");
System.out.println("Pattern is " + m.pattern());
while(m.find())
{
System.out.println(m.start() + " " + m.group());
// m.start() will give the index and m.group() will give the substring
}
}
}