Java regular expressions and dollar sign
You need to escape $
in the regex with a back-slash (\
), but as a back-slash is an escape character in strings you need to escape the back-slash itself.
You will need to escape any special regex char the same way, for example with ".".
String pattern = "/feedback/com\\.navteq\\.lcms\\.common\\.domain\\.poi\\.feedback\\.Review\\$0(.)*";
Java regex (java.util.regex). Search for dollar sign
You may use
String search = "/bla/$V_N.$XYZ.bla";
String pattern = "[%$]([^%.$]*)";
Matcher matcher = Pattern.compile(pattern).matcher(search);
while (matcher.find()){
System.out.println(matcher.group(1));
} // => V_N, XYZ
See the Java demo and the regex demo.
NOTE
- You do not need an optional
\1?
at the end of the pattern. As it is optional, it does not restrict match context and is redundant (as the negated character class cannot already match neither$
nor%
) [%$]([^%.$]*)
matches%
or$
, then captures into Group 1 any zero or more
chars other than%
,.
and$
. You only need Group 1 value, hence,matcher.group(1)
is used.- In a character class, neither
.
nor$
are special, thus, they do not need escaping in[%.$]
or[%$]
.
New line and dollar sign in Java regular expression
Your regex does not match the input string. In fact, $
matches the end of string (at the end of line3
). Since you are not using an s
flag, the .
cannot get there.
NOTE! that the $
anchor - even without Pattern.MULTILINE
option - can match a position before the final line feed char, see What is the difference between ^ and \A , $ and \Z in regex?. This can be easily tested with "a\nb\n".replaceAll("$", "X")
, resulting in "a\nbX\nX"
, see this Java demo.
More, the $
end of line/string anchor cannot have ?
quantifier after it. It makes no sense for the regex engine, and is ignored in Java.
To make it work at all, you need to use s
flag if you want to just return http://www.google.com
:
String test_domain = "http://www.google.com/path\nline2\nline3";
test_domain = test_domain.replaceFirst("(?s)(\\.[^:/]+).*$", "$1");
System.out.println(test_domain);
Output of this demo:
http://www.google.com
With a multiline (?m)
flag, the regex will process each line looking for a literal .
and then a sequence of characters other than :
and /
. When one of these characters is found, the rest of characters on that line will be omitted.
String test_domain = "http://www.google.com/path\nline2\nline3";
test_domain = test_domain.replaceFirst("(?m)(\\.[^:/]+).*$", "$1");
System.out.println(test_domain);
Output of this IDEONE demo:
http://www.google.com
line2
line3
How do you escape dollar and braces, (i.e. ${title}) in a java regular expression?
Not sure how the last one would result in an error; it'd just not match anything because you're using too many backslashes on the $
.
This should work:
string.replaceAll("\\$\\{title\\}", title);
How to get a dollar sign in Java regex
Here is a 2-step approach: we extract <...>
groups with a regex and then split the chunks into words and see if they start with $
.
String s = "<$FB $TWTR are getting plummetted>";
Pattern pattern = Pattern.compile("<([^>]+)>");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
String[] chks = matcher.group(1).split(" ");
for (int i = 0; i<chks.length; i++)
{
if (chks[i].startsWith("$"))
System.out.println(chks[i].substring(1));
}
}
See demo
And here is a 1-regex approach (see demo), use only if you feel confident with regex:
String s = "<$FB $TWTR are getting plummetted>";
Pattern pattern = Pattern.compile("(?:<|(?!^)\\G)[^>]*?\\$([A-Z]+)");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
System.out.println(matcher.group(1));
}
The regex used here is (?:<|(?!^)\G)[^>]*?\$([A-Z]+)
.
It matches:
(?:<|(?!^)\G)
- A literal<
and then at the end of each successful match[^>]*?
- 0 or more characters other than>
(as few as possible)\$
- literal$
([A-Z]+)
- match and capture uppercase letters (replace with what best suits your purpose, perhaps\\w
).
Java Regular Expression to match dollar amounts
Since you're doing this to learn regex...
^\$(([1-9]\d{0,2}(,\d{3})*)|(([1-9]\d*)?\d))(\.\d\d)?$
Breakdown:
^\$
start of string with $ a single dollar sign
([1-9]\d{0,2}(,\d{3})*)
1-3 digits where the first digit is not a 0, followed by 0 or more occurrences of a comma with 3 digits
or
(([1-9]\d*)?\d)
1 or more digits where the first digit can be 0 only if it's the only digit
(\.\d\d)?$
with a period and 2 digits optionally at the end of the string
Matches:
$4,098.09
$4098.09
$0.35
$0
$380
Does not match:
$098.09
$0.9
$10,98.09
$10,980456
String#replaceAll() method not replacing for $ (dollar)
String.replaceAll
is for regular expressions. '$'
is a special character in regular expressions.
If you are not trying to use regular expressions, use String.replace
, NOT String.replaceAll
.
Related Topics
How to Capture a Jframe's Close Button Click Event
Variable Naming Conventions in Java
How to Change Timezone for a Java.Util.Calendar/Date
Java, How to Implement a Shift Cipher (Caesar Cipher)
Validation of a List of Objects in Spring
Java - Swing Listen an Action in a Text Field of a Form
How to Use Mdc with Thread Pools
Jtable Won't Show Column Headers
Sending the Same But Modifed Object Over Objectoutputstream
Should Getters and Setters Be Synchronized
Comparing Arrays in Junit Assertions, Concise Built-In Way
How to Call a Method in Another Class of the Same Package
Find Place for Dedicated Application Folder
Difference Between Getclass().Getclassloader().Getresource() and Getclass.Getresource()
Access Restriction: Is Not Accessible Due to Restriction on Required Library ..\Jre\Lib\Rt.Jar