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_]+)\\$");
Related
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:
Java Find word in a String
(5 answers)
Find word in random string
(3 answers)
How to find index of whole word in string in java
(1 answer)
Regex to find a specific word in a string in java
(3 answers)
Closed 5 years ago.
I have a message that is of the format:
FixedWord1 variable1 FixedWord2 on FixedWord3 variable2/variable3, variable4 = variable5
I need to extract only variable3 from the above message.
Here is what I tried:
String example = "FixedWord1 variable1 FixedWord2 on FixedWord3 variable2/variable3, variable4 = variable5";
I know that the length of FixedWord3 is 6. So,
example.substring(example.lastIndexOf("FixedWord3") + 6 , example.lastIndexOf(",")); //To get {variable2}/{variable3}
And then,
String requiredString[] = example.split("/", 2); //requiredString[1] would contain {variable3} even if it contains /
Can you suggest a more efficient solution to this problem?
EDIT:
This regex should do the trick.
Pattern pattern = Pattern.compile(".+(Device).+[/]([A-Z].+)[,][ ].+");
Matcher matcher = pattern.matcher(yourstring);
if(matcher.matches())
System.out.println(matcher.group(2));
Assumption to make this work:
Variable2 has no slash '/' followed by upper case letter
Variable3 has no comma ',' followed by space ' '
Since you know that variable2 cannot contain a "/" and you know the length of FixedWord3 then how about this?
String deviceName = example.substring(example.lastIndexOf("Device") + 6, example.lastIndexOf(","));
String lastPart = deviceName.substring(deviceName.indexOf("/") + 1);
System.out.println(deviceName);
System.out.println(lastPart);
Prints:
SJ-ME3600X-185/GigabitEthernet0/4
GigabitEthernet0/4
Regex for the help.
One possible approach is catching the match that's after "{variable2}":
{variable2}\/{([^}]+)}
Then you can use Matcher and Pattern and maybe other tools to make it work in Java.
See here for explanation and live demo.
Using Regex patterns are the efficient way to extract the word from a message in java.
String s = "FixedWord1 {variable1} FixedWord2 on FixedWord3 {variable2}/{variable3}, {variable4} = {variable5}";
Pattern p = Pattern.compile("/(\\{([^}]*)\\})");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1));
}
Output {variable3}
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
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I want to know if there is a difference in these two regular expressions:
Pattern.compile("\"title\":\"(.*?)\"");
Pattern.compile("\"title\":\".*\"");
The part (.*?) and .* looks like they have the same meaning...
Here I get exactly the same results:
String title = null;
Pattern p = Pattern.compile("\"title\":\"(.*?)\"");
//Pattern p = Pattern.compile("\"title\":\".*\"");
Matcher m = p.matcher("sdfssdfsdfsdfsdf\"title\":\"Here is the title\"sdfgdfgdfgdfgdfg");
if (m.find()) {
title = m.group();
}
System.out.println(title);
Output:
"title":"Here is the title"
If I do not use parentheses - I'm still able to find separate groups like that:
Pattern p = Pattern.compile("\"title\":\".*?\"");
Matcher m = p.matcher("sdfssdfsdfsdfsdf\"title\":\"Here is the title\"dfdfgrt\"title\":\"Here is the title\"");
while (m.find()) {
System.out.println(m.group());
}
The output:
"title":"Here is the title"
"title":"Here is the title"
So - do I really need parentheses here?
There are two things here:
() --> Specifies a capturing group. So, if you want to capture something and want to refer to it later, you can use (what you want to capture here). Without the braces, you don't capture the data.
.* --> is greedy i.e, it tries to grab the entire string and goes one character less and tries to match again.
.*? --> is lazy (AKA reluctant) i.e, starts from length 0 and tries matching the string and stops at first match.
You could look at the official documentation here.
Try this code for matching without capturing
Pattern p = Pattern.compile("abc(.*?)");
Matcher m = p.matcher("abc");
while (m.find()) {
System.out.println("hi");
System.out.println("group1: " + m.group(1));
}
Output:
hi
group1:
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.