This question already has answers here:
How to make a regular expression match based on a condition?
(3 answers)
Closed 3 years ago.
Is that possible to compare the strings in regex with conditional operators(not in split type)
You can try this type of regex,
.*?(word1 word2 word3).*?\/\/\/.*?(?(1)\1)
Demo,,, in which the sub_string(\1, "word1 word2 word3") is checked if it is included in former string, and attempts to match the sub-string in the latter string.
It's the /// which used to sectionalize(or delimit) to the former and latter strings.
Related
This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 2 years ago.
I am new to java. I am practicing String methods currently. I wanted to check if the email contains "gmail.com" or not.
This is the code I came up with
System.out.println(domain.matches(".*gmail.com(.*)"));
but dot(.) here means any character so even if i pass the string as "xyz#gmailpcom" it will return true. Basically I want to check dot(.) in the string without considering it as regular expression.
If you just want to check whether it contains gmail.com, then just use contains:
System.out.println(domain.contains("gmail.com"));
If at a later stage you want to use a regular expression but escape a dot, do that with a backslash in the regular expression, which needs to be escaped again for use in a Java string literal:
domain.matches(".*gmail\\.com(.*)")
you can also check it bu checking the indexOf or lastIndexOf:
System.out.println(domain.indexOf("gmail.com") != -1);
System.out.println(domain.lastIndexOf("gmail.com") != -1);
This question already has answers here:
Alternation operator inside square brackets does not work
(2 answers)
What is the difference between square brackets and parentheses in a regex?
(3 answers)
Closed 2 years ago.
I want to replace all special characters in the string shown below:
String a="Test’‵"
I want to replace ’ and ‵ with dashes (-). I have tried the following:
a=a.replaceAll("[’|‵]", "-");
This generates the following result:
Test------
instead of
Test--
How can I achieve the desired result?
Don't use square brackets, as it represents a set of single characters to match (a character class).
a=a.replaceAll("’|‵", "-");
Demo!
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Regex to first occurrence only? [duplicate]
(4 answers)
Closed 4 years ago.
I've tested this regex:
String regex = "[e][#](.)+[$]
the regex works well to identify exponential terms, however it breaks when there are two or more consecutive exponential terms.
I test the regex with the usual code:
while(matcher.find()){
String string = matcher.group();
System.out.println("this one: "+string);
}
When I type the expression:
e#x$ + 3e#x+1$
string equals to (e#(x$+3e#x+1$))
By the way, I added the parentheses inside the while loop. They are necessary for
what I am trying to accomplish.
I want the result of string to be (e(#x$))+3(e(#x+1$)
I know the problem lies in "(.)+ What I think is happening is that the regex
includes the first $, what I need is for it to stop at the first $.
How can I include this logic inside the regex?
thank you
This question already has answers here:
Why is String.matches returning false in Java?
(3 answers)
Closed 5 years ago.
Pattern.matches("[A(BC)]", "BC") why this returns false?
Because the pattern expects to see a single character from the class A(BC), and matches matches the entire input against the regex (doesn't look for partial matches). Since the input is two characters, it isn't a match.
This question already has answers here:
Split string to equal length substrings in Java
(23 answers)
Closed 6 years ago.
I'd like to split the string into substrings which has 20 chars (or less for the tail). Is there some library or I need to make the class for that?
you should use :
s.split("(?<=\\G.{20})");
\G is a zero-width assertion that matches the position where the previous match ended. If there was no previous match, it matches the beginning of the input, the same as \A. The enclosing lookbehind matches the position that's 20 characters along from the end of the last match.
Or, with Groovy you could do:
assert 'abcdefghij'.toList().collate( 3 )*.join() == ['abc', 'def', 'ghi', 'j']