This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 6 years ago.
I wanted to extract what ever is within the below tokens
${FNAME} ${LNAME} ${123}
FNAME LNAME 123.
I tried the below.
public static void main(String[] args) {
String input = "{FNAME} ${LNAME} ${123}";
Pattern p = Pattern.compile("\\$\\{");
Matcher m = p.matcher(input);
while (m.find()) {
System.out.println("Found a " + m.group() + ".");
}
}
Ended up wrongly. Beginner to reg expressions.
You should use lazy quantifier ? and capture group () like this.
Regex: \$\{(.*?)\}
Replacement to do: \1 for first captured group.
Regex101 Demo
Related
This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 3 years ago.
I have a string in java Select * from tbl_Name tn where tn.PublicID='?'and I am trying to find the occurrence of .PublicID='?' considering case insensitivity.
I am using below regex expression (?i).PublicID[ ]?=\'\?\', but it is not able to find any match in above string
Java Snippet used
String query = "";
if(queryOrTable.equalsIgnoreCase("Query"))
{
Pattern pattern = Pattern.compile("(?i).PublicID[ ]?=\\'\\?\\'");
Matcher matcher = pattern.matcher(tblName);
if(matcher.matches())
{
query = query.replaceAll("(?i).PublicID[ ]?=\\'\\?\\'", ".PublicID='" + publicID + "'");
}
else
{
System.out.println("malformed query");
}
}
That should work.
Tested here: https://www.freeformatter.com/java-regex-tester.html
Now, instead of [ ]? matching one space, I'd use \s*.
You are matching one optional space with yours, I'm saying match 0 or more whitespace characters (which will work for more cases of SQL than what you're looking for.
This question already has answers here:
Regex whitespace word boundary
(3 answers)
Closed 3 years ago.
public static void main(String args[]) {
findExactWord find = new findExactWord();
String fullString = "reports of a chemical (reaction; in the kitchen) area found a male employee suffering from nausea";
System.out.println(find.isContainExactWord(fullString, "chemical (reaction; in the kitchen)"));
}
private boolean isContainExactWord(String fullString, String partWord){
String pattern = "\\b"+partWord+"\\b";
System.out.println("Pattern : "+partWord);
Pattern p=Pattern.compile(pattern);
Matcher m=p.matcher(fullString);
return m.find();
}
I want this result to be - true.
Search input is : "chemical (reaction; in the kitchen)
this should search all characters exactly as is.
output is now : false
String pattern = partWord;
System.out.println("Pattern : " + partWord);
Pattern p = Pattern.compile(pattern, Pattern.LITERAL);
Matcher m = p.matcher(fullString);
return m.find();
now the tested version ;-)
it matches special characters and ignores newlines
This question already has answers here:
Is it possible to match nested brackets with a regex without using recursion or balancing groups?
(2 answers)
Closed 5 years ago.
I am trying to write a regex for delimiters “(“, “)”, “,”. I tried to write a regex but it is not the correct for the delimeters.
Let's say the input is mult(3,add(2,subs(4,3))). The output with my delimeter regex is: 3,add(2,subs(4,3.
public class Practice {
private static final String DELIMETER = "\\((.*?)\\)";
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
String arg = reader.next();
Pattern p = Pattern.compile(DELIMETER);
Matcher m = p.matcher(arg);
while (m.find()) {
System.out.println(m.group(1));
}
}
}
What is the correct regex to get string between the delimeters?
In general, you cannot use a regex to match anything which can nest recursively. However, if you removed the ? from your regex, it would match from the first ( to the last ), which might be good enough, depending on what you expect the input to look like.
This question already has answers here:
Regex to replace all string literals in a Java file
(4 answers)
Closed 8 years ago.
The sample source code to match is
String string="welcome";
String k="a\"welcome";
I am using "(\"[^(\")]*\")" regex in java.
But this extracts
0:"welcome"
0:"a\"
Expected output is
0:"welcome"
0:"a\"welcome"
What change should i make in regex to get the expected output ?
Java source :
private static String pattern1="(\"[^(\")]*\")";
public void getStrings(){
Pattern r = Pattern.compile(pattern1);
Matcher m = r.matcher("String string=\"welcome\";\n" +
"String k=\"a\\\"welcome\";");
while(m.find()){
System.out.println("0:"+m.group(0));
}
}
Just use lookahead and lookbehind in your regex,,
(?<==)(".*?")(?=;)
Get the value from group index 1.
DEMO
Pattern r = Pattern.compile("(?<==)(\".*?\")(?=;)");
Matcher m = r.matcher("String string=\"welcome\";\n" +
"String k=\"a\\\"welcome\";");
while(m.find()){
System.out.println("0:"+m.group(1));
}
Output:
0:"welcome"
0:"a\"welcome"
OR
Use the greediness of *,
Pattern r = Pattern.compile("(\".*\")");
OR
It skips the double quotes which are preceded by a backslash,
Pattern r = Pattern.compile("(\\\".*?(?<=[^\\\\])\\\")");
Why do you even bother with variable assignment. You know that everything within "" is a string.
"(.+)"\s*; should do it just fine.
This question already has answers here:
Java regular expressions and dollar sign
(5 answers)
Closed 8 years ago.
I'm trying pattern matching expression for a below string. But it doesn't work. could you anybody help me on this ? Only Alphanumeric and underscore allowed inside,Both side
$ sign will be there. Ex strings: Test_1,23_test_2,test3.
String text = "$test_1$";
Pattern p = Pattern.compile("$([A-Za-z0-9_])$");
Matcher m = p.matcher(text);
m.matches();
if (m.find()) {
System.out.println("Matched: " + m.group(1));
} else {
System.out.println("No match.");
}
Your regex should be:
Pattern p = Pattern.compile("(\\$[A-Za-z0-9_]*\\$)");
You could simply do...
s.matches("\\$[a-zA-Z0-9_]*\\$")
$ is a regex meta character and should be escaped, try this
Pattern p = Pattern.compile("\\$([A-Za-z0-9_]+)\\$");