Given such Regex code:
Matcher m = Pattern.compile("c:.*?(|t:){1}.*?").matcher(string);
I only want to match something like c:somesubstring|t:somesubstring. However it also matches some thing like this:
c:somesubstring
and
c:somesubstring|a:somesubtring
How could this come? I use (|t:){1} to guarantee that the pattern |t: occurs and occurs only once. Will be helpful to tell me what's wrong with my regex and give me a regex to match only c:somesubstring|t:somesubstring
| is a special meta character in regex which acts like a logical OR operator usually used to combine two regexes . You need to escape the | symbol, so that it would match a literal | symbol.
Matcher m = Pattern.compile("c:.*?(\\|t:){1}.*?").matcher(string);
much shorter.
Matcher m = Pattern.compile("c:.*?\\|t:.*?").matcher(string);
Related
I created this regex, but somehow it only detects the first part
of the regex not the last part. I would like to know what is going on?
Here's the code:
String m = -2√3254i/18.5
String regex = "-?\\d+(\\.\\d*)?\\√\\d+(\\.\\d*)?i\\/\\d+(\\.\\d*)?"
I have tried many different ways, such as:
-?\\d+(\\.\\d*)?\\√\\d+(\\.\\d*)?i+\\/+\\d+(\\.\\d*)?
-?\\d+(\\.\\d*)?\\√\\d+(\\.\\d*)?i/\\d+(\\.\\d*)?
-?\\d+(\\.\\d*)?\\√\\d+(\\.\\d*)?i\\(\\/\\d+(\\.\\d*))?
none of them work.
the output is always
-2√3254
Any suggestions,
thank you
Okay so my regex is actually composed of many regexes:
String regex = "a regex | another regex | -?\\d+(\\.\\d*)?\\√\\d+(\\.\\d*)?\\i"
+ "|another regex | -?\\d+(\\.\\d*)?\\√\\d+(\\.\\d*)?i\\/\\d+ (\\.\\d*)?
The problem happens between both regexes shown. The first symbolical regex is picked up by the matcher first, but I really intended for the second symbolical regex to pick up my String m = "-2√32454i/18.5"
It seems the matcher exits matching when one of the boolean conditions is met.
All I had to do was rearrange the order of my regexes which make up my regex.
I was trying use a regex to find some matches in a string in Java. The actual regex is
^(interface \X*!)
When i do it Java i use
^(interface \\X*!)
Now this throws Illegal/unsupported escape sequence near index 13. I searched the boards a little bit and found that it should actually be four backslashes to make it work. But if i use
^(interface \\\\X*!)
it returns no matches. Any pointers would be really helpful.
Just a sample match would be like
interface ABC
temp
abc
xyz
!
The \X construct comes from Perl, and the Javadoc for java.util.Pattern explicitly states in the section Comparison to Perl 5 that it is not supported.
In Java, you have to use a different construct. But this part is already answered in https://stackoverflow.com/a/39561579.
In order to match the pattern you identify in the comments, using Java, something like this should work:
Pattern p = Pattern.compile("interface[^!]*!", Pattern.DOTALL);
Matcher m = p.matcher("interface ABC\ntemp\nabc\nxyz\n!"); // your test string
if (m.matches()) {
//
}
This pattern matches any string beginning with "interface", followed by zero or more of any character except "!", followed by "!".
Pattern.DOTALL tells it that in addition to all other characters, "." should also match carriage returns and line feeds. See this for more info on DOTALL.
I want to check the filenames sent to me against two patterns.
The first regular expression is ~*~, which should match names like ~263~. I put this in online regular expression testers and it matches. The code doesnt work though. Says no match
List<FTPFile> ret = new ArrayList<FTPFile>();
Pattern pattern = Pattern.compile("~*~");
Matcher matcher;
for (FTPFile file : files)
{
matcher = pattern.matcher(file.getName());
if(matcher.matches())
{
ret.add(file);
}
}
return ret;
Also the second pattern I need is ##* which should match strings like abc#ere#sss
Please tell me the proper patterns in java for this.
You need to define your pattern like,
Pattern pattern = Pattern.compile("~.*~");
~* in your regex ~*~ will repeat the first ~ zero or more times. So it won't match the number following the first ~. Because matches method tries to match the whole input string, this regex causes the match to fail. So you need to add .* inbetween to match strings like ~66~ or ~kjk~ . To match the strings which has only numbers present inbetween ~, you need to use ~\d+~
Try Regex:
\~.*\~
Instead:
~*~
Example:
Pattern pattern = Pattern.compile("\\~.*\\~");
I'm trying to replace all occurences in a string with a regex using a Pattern object, but it only replaces the odd occurences:
final Pattern p = Pattern.compile("(^|\\W|\\\\N)(recursive)(\\W|$)", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
System.out.println(p.matcher("i-i-i").replaceAll("$1I$3"));
This returns me:
I-i-I
But I need to to match also the I in the middle, but somehow it doesn't catch that. I also tried a simplified regex (^|-)(I)($|-) and try to do the same with i-i-i-i-i-i which returned me I-i-I-i-I-i.
I guess it is because the odd dashs (at 4x+1) were already matched, so they can't be matched a second time for the even is. Is it possible to allow that?
It seems that your problem is that you are trying to use same character - in few matches. In that case you should probably use look-around mechanism. For example you can change your
(^|-)(I)($|-)
patter to
(^|-)(I)(?=($|-))
and as replacement use $1I. This way regex will only check if after I exists $ or - but will not include it in match, so
final Pattern p = Pattern.compile("(^|-)(I)(?=($|-))",
Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
System.out.println(p.matcher("i-i-i-i-i-i").replaceAll("$1I"));
prints
I-I-I-I-I-I
How do I combine ch..+ and ch..- in regexp effectively without having to scan separately?
And are we using matcher in the pattern?
My output code is like this:
ch01+
ch01-
ch02+
ch02-
...
How do I combine ch..+ and ch..- in regexp effectively without having to scan separately?
Use | (pipe) for alternation:
ch..(\+|-)
And are we using matcher in the pattern?
Depends on how you're using the regexp and the pattern. To get a concrete answer, you'll have to show some actual code, or ask a much more specific question.
N.B. If you want to restrict the two characters after ch to 0-9, you can use \d, which is a shorthand character class for [0-9]:
ch\d{2}(\+|-)
You can use a character class containing just "+" and "-" like so "[+-]".
Pattern p = Pattern.compile("ch..[+-]");
Matcher m = p.matcher("ch01+");
if (m.find()) {
// found it...