Finding the character "^" using regex [duplicate] - java

This question already has an answer here:
Escaping special characters in java regex (not quoting)
(1 answer)
Closed 8 years ago.
I'm trying to define a regex pattern that searches for a caret character, but since ^ is used for negation, I'm not sure how to define the pattern. I'm trying to make the program find a string that is a letter then a caret then a number (as you may have guessed, this is a mathematical term), such as "x^23". This is the line I tried:
String caseFour = "[a-zA-Z]" + "^" + "\\d+";
It's not working. Can anyone help me out?

You need to escape that character also since it is a character of special meaning.
String regex = "[a-zA-Z]\\^\\d+";

Related

Correct argument for useDelimiter to ignore empty space as well as two specific signs [duplicate]

This question already has answers here:
How do I use a delimiter with Scanner.useDelimiter in Java?
(3 answers)
Closed 5 years ago.
I want the scanner to ignore three things: empty spaces, "/" and "!".
What is the correct argument to use in the useDelimiter method?
useDelimiter takes a regex argument docs:
pattern - A string specifying a delimiting pattern
So just make sure the string is in regex form.
Whitespace in regex is \s, escape that to become \\s. / is still / and ! is still !. You then use | to act as an "or" operator to say "either one of these".
Here's how to do it:
scanner.useDelimiter("\\s|/|!");
If you want to say that "consecutive whitespaces slashes and exclamation marks also count as delimiter", then you can add a quantifier + to the whole thing:
scanner.useDelimiter("(\\s|/|!)+");
Scanner's delimiter is just a pattern, so you could use the following:
sc.useDelimiter("[\\s/!]*");

This pattern matches for input 123456789.2.2.2 , which it should not [duplicate]

This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 6 years ago.
I am trying to solve following task:
Match the pattern abc.def.ghi.jkl, where each variable a,b,c,d,e,f,g,h,i,j,k,l can be any single character except the newline.
For above question I am matching the input to regex :
"([^\\n]{3}(.)){3}([^\\n]{3})"
// this is the regex pattern I am using currently
What am I doing wrong? Please help me correct the above regex so that it does not match the incorrect input I have provided in the title. Currently it matches to it somehow. Although I have provided 3 it is apparently matching to more than 3 characters.
. has a special meaning in regular expression patterns.
If you want to get a "simple dot", you need to quote/escape it (as "\\.").
And that special meaning is (under normal configuration) "any character except line breaks", which exactly matches your other condition, so you can simplify this to
"(...)\\.(...)\\.(...)\\.(...)"

Require character escapes regex [duplicate]

This question already has answers here:
Match key-value pattern regex
(2 answers)
Closed 8 years ago.
I am attempting to build a key-value serializer and I need to match all characters except =, ", , and \ when not prefixed with a backslash.
So far what I have to match the rest of the pattern is with 'here' being where I need to match the 'special' characters ^[[a-zA-Z0-9-]+[=][^here]]+$
This pattern matches all such characters:
(?<!\\)[="'\\]
In java, the string literal is ugly with all the escapes:
String regex = "(?<!\\\\)[=\"'\\\\]";

Java regex ignore '.' symbol [duplicate]

This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 9 years ago.
I'm trying to split a string at every '.' (period), but periods are a symbol used by java regexes. Example code,
String outstr = "Apis dubli hre. Agro duli demmos,".split(".");
I can't escape the period character, so how else do I get Java to ignore it?
Use "\\." instead. Just using . means 'any character'.
I can't escape the period character, so how else do I get Java to ignore it?
You can escape the period character, but you must first consider how the string is interpreted.
In a Java string (that is fed to Pattern.compile(s))...
"." is a regex meaning any character.
"\." is an illegally-escaped string. This won't compile. As a regex in a text editor, however, this is perfectly legitimate, and means a literal dot.
"\\." is a Java string that, once interpreted, becomes the regular expression \., which is again the escaped (literal) dot.
What you want is
String outstr = "Apis dubli hre. Agro duli demmos,".split("\\.");

How to write a regex that prevents partial matching [duplicate]

This question already has answers here:
Regex whitespace word boundary
(3 answers)
Closed 2 years ago.
How do I build a regex pattern that searches over a text T and tries to find a search string S.
There are 2 requirements:
S could be made of any character.
S could be anywhere in the string but can't be part of a word.
I know that in order to escape special regex characters I put the search string between \Q and \E as such:
\EMySearch_String\Q
How do I prevent finding partial matching of S in T?
You can do like this if
can't be part of a word
is interpreted as
preceded by start-of-string or space and followed by end-of-string or space:
String s = "3894$75\\/^()";
String text = "fdsfsd3894$75\\/^()dasdasd 22348 3894$75\\/^()";
Matcher m = Pattern.compile("(?<=^|\\s)\\Q" + s + "\\E(?=\\s|$)").matcher(text);
while (m.find()) {
System.out.println("Found match! :'" + m.group() + "'");
}
This prints only one
Found match! :'3894$75/^()'
I think what you're trying to find can be easily solved with lookaheads and lookbehinds. Take a look at this for a good explanation.
Then there's a bit of flip-flopping booleans, but you're looking ahead and behind for NOT Non-Space characters (\S). You don't want to look for space characters only because S might be at the start or end of the string. Like so:
(?<!\S)S(?!\S)

Categories

Resources