Could you please tell me why this expression does not return TRUE?
public class test {
public static void main(String[] args) throws IOException{
String str = "The dog plays";
boolean t = str.matches("\\bdog\\b");
System.out.println(t);
}
}
matches method will always try to match the whole string. It won't be a suitable method for matching a particular string. So change your regex to,
".*\\bdog\\b.*"
in-order to make the matches method to return true.
String str = "dog plays";
System.out.println(str.matches(".*\\bdog\\b.*"));
Output:
true
\b called word boundary which matches between a word character and a non-word character. Note that the above regex will match also the string foo:dog:bar. If you want the dog to be a separate word , i suggest you to use this regex.
".*(?<!\\S)dog(?!\\S).*"
Example:
System.out.println("dog plays".matches(".*(?<!\\S)dog(?!\\S).*"));
System.out.println("foo:dog:bar".matches(".*(?<!\\S)dog(?!\\S).*"));
Output:
true
false
For the Matcherclass:
matches() returns true if the entire string matches the expression
find() returns true if any subsequence of the string matches the expression.
So it's likely that this is what you want:
public class Test {
public static void main(String[] args) {
String str = "The dog plays";
boolean t = str.find("\\bdog\\b");
System.out.println(t);
}
}
Related
I have the following input:
8=FIX.4.2|9=00394|35=8|49=FIRST|8=FIX.4.2|9=00394|35=8|56=MIDDLE|10=245|8=FIX.4.2|9=00394|35=8|49=LAST|56=HEMADTS|10=024|
Now I want the strings that are starting with "8=???" and end with "10=???|". You can see above that there are exactly two strings that start with 8 and end with 10. I have written a program for this.
Below is my code:
public class Main {
static Pattern r = Pattern.compile("(.*?)(8=\\w\\w\\w)[\\s\\S]*?(10=\\w\\w\\w)");
public static void main(String[] args) {
String str = "8=FIX.4.2|9=00394|35=8|49=FIRST|8=FIX.4.2|9=00394|35=8|56=MIDDLE|10=245|8=FIX.4.2|9=00394|35=8|49=LAST|56=HEMADTS|10=024|";
match(str);
}
public static void match(String message) { //send to OMS
Matcher m = r.matcher(message);
while (m.find()) {
System.out.println(m.group());
}
}
}
When I just run this I am getting the wrong output like:
8=FIX.4.2|9=00394|35=849=FIRST`|8=FIX.4.2|9=00394|35=8|56=MIDDLE|10=245|`
8=FIX.4.2|9=00394|35=849=LAST|56=HEMADTS|10=024|
You can see the first string in the output. It consists of "8=???" two times but the exact output needs to be like:
8=FIX.4.2|9=00394|35=8|56=MIDDLE|10=245|
8=FIX.4.2|9=00394|35=849=LAST|56=HEMADTS|10=024|
I also want the un-matched strings in separate as there is a further work with those strings. How can I get that? So, the total output needs to be like:
Matched : 8=FIX.4.2|9=00394|35=8|56=MIDDLE|10=245|
Matched : 8=FIX.4.2|9=00394|35=849=LAST|56=HEMADTS|10=024|
UnMatched : 8=FIX.4.2|9=00394|35=849=FIRST`|
You need to use a tempered greedy token to match the shortest window possible between 2 strings. That will solve the first problem. To get unmatched strings, just split the string with the pattern.
Use
\b8=\w{3}(?:(?!8=\w{3})[\s\S])*?10=\w{3}\|
See the regex demo.
Details
\b - a word boundary
8= - a literal substring
\w{3} - 3 word chars
(?:(?!8=\w{3})[\s\S])*? - a tempered greedy token matching any char ([\s\S]), zero or more times, as few as possible, that do not start a 8= and 3 word chars pattern
10= - a literal substring
\w{3} - 3 word chars
\| - a literal |.
Java code:
public static Pattern r = Pattern.compile("\\b8=\\w{3}(?:(?!8=\\w{3})[\\s\\S])*?10=\\w{3}\\|");
public static void main (String[] args) throws java.lang.Exception
{
String str = "8=FIX.4.2|9=00394|35=8|49=FIRST|8=FIX.4.2|9=00394|35=8|56=MIDDLE|10=245|8=FIX.4.2|9=00394|35=8|49=LAST|56=HEMADTS|10=024|";
match(str);
}
public static void match(String message) { //send to OMS
Matcher m = r.matcher(message);
System.out.println("MATCHED:");
while (m.find()) {
System.out.println(m.group());
}
System.out.println("UNMATCHED:");
String[] unm = r.split(message);
for (String s: unm) {
System.out.println(s);
}
}
See the Java demo.
Results:
MATCHED:
8=FIX.4.2|9=00394|35=8|56=MIDDLE|10=245|
8=FIX.4.2|9=00394|35=8|49=LAST|56=HEMADTS|10=024|
UNMATCHED:
8=FIX.4.2|9=00394|35=8|49=FIRST|
When use java regular-expression pattern.matcher(), source does not match regex.But, my hope result is ,source matches regex.
String source = "ONE.TWO"
String regex = "^ONE\\.TWO\\..*"
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
test();
}
public static void test() {
Test stringDemo = new Test();
stringDemo.testMatcher();
}
public void testMatcher() {
String source = "ONE.TWO";
String regex = "^ONE\\.TWo\\..*";
// The result = false, "not match". But, the hope result is true, "match"
matcher(source, regex);
}
public void matcher(String source, String regex) {
Pattern pattern = Pattern.compile(regex);
boolean match = pattern.matcher(source).matches();
if (match) {
System.out.println("match");
} else {
System.out.println("not match");
}
}
}
In your code, your regular expression expects the o in TWO to be lower case and expects it to be followed by a ..
Try:
String source = "ONE.TWo.";
This will match your regular expression as coded in your question.
The expression \. means match a literal dot (rather than any character). When you code this into a Java String, you have to escape the backslash with another backslash, so it becomes "\\.".
The .* on the end of the expression means "match zero or more of any character (except line-break)".
So this would also match:
String source = "ONE.TWo.blah blah";
Well it doesn't match for two reasons:
Your regex "^ONE\\.TWo\\..*" isn't case sensitive so how do you expect TWo to match TWO.
And your regex expects a . character at the end while your string "ONE.TWO" doesn't have it.
Use the following Regex, to match your source string:
String regex = "^ONE\\.TWO\\.*.*";
Pattern matching is case sensitive by Default. In your case source has a uppercase O and regex a lowercase o.
So you have to add Pattern.CASE_INSENSITIVE or Change the case of o
Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE );
or
String regex = "^ONE\\.TWO\\..*";
Your regex is a bit incorrect. You have an extra dot here:
String regex = "^ONE\.TWO\.(extra dot).*"
Try this one, without dot:
String regex = "^ONE\.TWO.*"
String regex = "^ONE\\.TWO\\..*"
The DOUBLE SLASH \\ in regex is escape sequence to match a SINGLE SLASH \ in Source string.
The .* at the end matches any character 0 or More times except line breaks.
To match the regex your source should be like
String source = "ONE\.TWO\three blah ##$ etc" OR
String source = "ONE\.TWO\.123##$ etc"
Basically its Any String which starts with ONE\.TWO\ and without line breaks.
I have String like this "abcdefgh"
I want to check the string contains the following characters [fcb]
Condition is : The string must contain all characters in any order.
How to write a regex for this one.
I tried following regexes :
.*[fcb].* ---> In this case it not check all characters. If any one character matchs it will return true
Don't use regex. Just use String.contains to test for each of the characters in turn:
in.contains("f") && in.contains("c") && in.contains("b")
You could get the char arry and sort it. Afterwards you could check if it contains .*b.*c.*f.*.
public static boolean contains(String input) {
char[] inputChars = input.toCharArray();
Arrays.sort(inputChars);
String bufferInput = String.valueOf(inputChars);
// Since it is sorted this will check if it simply contains `b,c and f`.
return bufferInput.matches(".*b.*c.*f.*");
}
public static void main(String[] args) {
System.out.println(contains("abcdefgh"));
System.out.println(contains("abdefgh"));
}
output:
true
false
this will check if all the letters are present in the string.
public class Example {
public static void main(String args[]) {
String stringA = "abcdefgh";
String opPattern = "(?=[^ ]*f)(?=[^ ]*c)(?=[^ ]*b)[^ ]+";
Pattern opPatternRegex = Pattern.compile(opPattern);
Matcher matcher = opPatternRegex.matcher(stringA);
System.out.println(matcher.find());
}
}
You can use positive lookahead for this
(?=.*b)(?=.*c)(?=.*f)
Not very efficient but easy to understand:
if (s.matches(".*b.*") && s.matches(".*c.*") && s.matches(".*f.*"))
I want to check if a string contains "\" or not. I used the string method contains(), but its not working
String path = "D:\steve\";
if(path.contains("\"){
path = path.replaceAll("\\\\","");
}
Use escape character \
public class test{
public static void main(String[] args) {
String str = "a\\b";
System.out.println(str.contains("\\"));
}
}
A character preceded by a backslash (\) is an escape sequence and has
special meaning to the compiler.
https://docs.oracle.com/javase/tutorial/java/data/characters.html
I am quite bad at Java Regular expression so I hope you guys will help me.
String variable = "My life is better ";
String variable2 = "My life01 is better";
Now I have to write a code which would return true if the string has only "life"
So I should get TRUE only for variable not for variable2 because it has life but "01" too.
~thanks.
I have tried
if (variable.contains("life")){
System.out.println("TRUE");}
It return TRUE for both.
See solution :
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Pattern p = Pattern.compile("\\blife\\b");
Matcher m = p.matcher("life0 is better");
boolean b = m.find();
System.out.println(b);
}
}
Use word boundary \b matches.
See the Java Pattern documentation for details.
Note that you may need to write it at \\b to get proper escaping. The pattern needs the string \b, which when used in .java code (and not read e.g. from a file!) needs to be written in Java-escaped form as "\\blife\\b".
Use the following regex: -
"\blife\b"
with Pattern and Matcher class. This will match for complete word. (\b denote word boundary)
You would have to use Matcher#find method, to check whether a string contains this pattern.
Note: - If you want to use String.matches, which would be appropriate here, than going with Pattern and Matcher, you would have to add .* in the front and the end. Because, String.matches matches the whole string.
For e.g: -
String str = "asdf life asdf";
System.out.println(str.matches("\\blife\\b")); // Prints false
System.out.println(str.matches(".*\\blife\\b.*")); // Prints true
In the second Regex, .* matches the string before and after life.
Use the String.matches() method and Regex \b to match word boundaries.
public class StringChecker {
String variable1 = "My life is better ";
String variable2 = "My life01 is better";
System.out.println("variable1: " + containsString(variable1));
System.out.println("variable1: " + containsString(variable2));
//Returns true if the string contains "life"
public boolean containsString(String s){
return s.matches(".*(\\blife\\b).*");
}
}
package com.rampukar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DemoRegx {
public static void main(String[] args) {
String email = "ram#example.com";
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pt_mail = Pattern.compile(EMAIL_PATTERN);
Matcher mt_mail = pt_mail.matcher(email);
if (mt_mail.matches()) {
System.out.println("Valid");
} else {
System.out.println("In-Valid");
}
System.out.println("---------------------");
String name = "Ram Pukar";
Pattern pt_name = Pattern.compile("[a-zA-Z ]{2,}");
Matcher mt_name = pt_name.matcher(name);
if (mt_name.matches()) {
System.out.println("Valid");
} else {
System.out.println("In-Valid");
}
System.out.println("---------------------");
String user = "ram123";
Pattern pt_user = Pattern.compile("[a-zA-Z0-9]{2,}");
Matcher mt_user = pt_user.matcher(user);
if (mt_user.matches()) {
System.out.println("Valid");
} else {
System.out.println("In-Valid");
}
}
}