Using Pattern and Matcher to search for special characters (Example: $)
Apologies if this has already been answered.
I am using the following code to search for a substring:
String subject = "ABC"
String subString = "AB"
Pattern pattern = Pattern.compile(subString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(subject);
while (matcher.find()){
//Matched
}
But when my subject string contains a $ in the beginning, it does not work since it is a special character.
String subject = "$ABC"
String subString = "$"
How does one handle that?
java regex pattern-matching
add a comment |
Apologies if this has already been answered.
I am using the following code to search for a substring:
String subject = "ABC"
String subString = "AB"
Pattern pattern = Pattern.compile(subString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(subject);
while (matcher.find()){
//Matched
}
But when my subject string contains a $ in the beginning, it does not work since it is a special character.
String subject = "$ABC"
String subString = "$"
How does one handle that?
java regex pattern-matching
add a comment |
Apologies if this has already been answered.
I am using the following code to search for a substring:
String subject = "ABC"
String subString = "AB"
Pattern pattern = Pattern.compile(subString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(subject);
while (matcher.find()){
//Matched
}
But when my subject string contains a $ in the beginning, it does not work since it is a special character.
String subject = "$ABC"
String subString = "$"
How does one handle that?
java regex pattern-matching
Apologies if this has already been answered.
I am using the following code to search for a substring:
String subject = "ABC"
String subString = "AB"
Pattern pattern = Pattern.compile(subString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(subject);
while (matcher.find()){
//Matched
}
But when my subject string contains a $ in the beginning, it does not work since it is a special character.
String subject = "$ABC"
String subString = "$"
How does one handle that?
java regex pattern-matching
java regex pattern-matching
asked Nov 20 '18 at 1:34
SunnySunny
2,700144578
2,700144578
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
By escaping the special character in the subString
. Like,
String subString = "\$";
or telling the Pattern
to match literals. Like,
Pattern pattern = Pattern.compile(subString, Pattern.LITERAL | Pattern.CASE_INSENSITIVE);
1
If you don't know in advance what you're searching for,Pattern.quote
is your friend.
– Amadan
Nov 20 '18 at 1:43
add a comment |
There are few meta characters in regex. And some of them which are supported by regex in java are
( ) [ ] { { ^ $ | ? * + . < > - = !
So $ is a indeed meta character here. The meta character conveys special meaning to the regex engine and hence can't be use literally. So in order to use them you have to combine them with escape character which is backslash
So String subject = "\$ABC"
String subString = "\$"
would do. Java uses double backslash instead of single for escape character unlike the other regex engine.
2
"Java uses double backslash instead of single" is incorrect; Java uses single backslash just like other regexes. For example, if you look at your "subject" or "substring" variables in the debugger as the program is running, you would find the string value contains a single backslash. The need for the double-backslash is purely to escape the backslash for a String value (similar to "n", etc), not for the Regex.
– racraman
Nov 20 '18 at 1:54
yes. My bad I just forget it is the case of escape character
– Brij Raj Kishore
Nov 20 '18 at 1:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385004%2fusing-pattern-and-matcher-to-search-for-special-characters-example%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
By escaping the special character in the subString
. Like,
String subString = "\$";
or telling the Pattern
to match literals. Like,
Pattern pattern = Pattern.compile(subString, Pattern.LITERAL | Pattern.CASE_INSENSITIVE);
1
If you don't know in advance what you're searching for,Pattern.quote
is your friend.
– Amadan
Nov 20 '18 at 1:43
add a comment |
By escaping the special character in the subString
. Like,
String subString = "\$";
or telling the Pattern
to match literals. Like,
Pattern pattern = Pattern.compile(subString, Pattern.LITERAL | Pattern.CASE_INSENSITIVE);
1
If you don't know in advance what you're searching for,Pattern.quote
is your friend.
– Amadan
Nov 20 '18 at 1:43
add a comment |
By escaping the special character in the subString
. Like,
String subString = "\$";
or telling the Pattern
to match literals. Like,
Pattern pattern = Pattern.compile(subString, Pattern.LITERAL | Pattern.CASE_INSENSITIVE);
By escaping the special character in the subString
. Like,
String subString = "\$";
or telling the Pattern
to match literals. Like,
Pattern pattern = Pattern.compile(subString, Pattern.LITERAL | Pattern.CASE_INSENSITIVE);
answered Nov 20 '18 at 1:37
Elliott FrischElliott Frisch
154k1393182
154k1393182
1
If you don't know in advance what you're searching for,Pattern.quote
is your friend.
– Amadan
Nov 20 '18 at 1:43
add a comment |
1
If you don't know in advance what you're searching for,Pattern.quote
is your friend.
– Amadan
Nov 20 '18 at 1:43
1
1
If you don't know in advance what you're searching for,
Pattern.quote
is your friend.– Amadan
Nov 20 '18 at 1:43
If you don't know in advance what you're searching for,
Pattern.quote
is your friend.– Amadan
Nov 20 '18 at 1:43
add a comment |
There are few meta characters in regex. And some of them which are supported by regex in java are
( ) [ ] { { ^ $ | ? * + . < > - = !
So $ is a indeed meta character here. The meta character conveys special meaning to the regex engine and hence can't be use literally. So in order to use them you have to combine them with escape character which is backslash
So String subject = "\$ABC"
String subString = "\$"
would do. Java uses double backslash instead of single for escape character unlike the other regex engine.
2
"Java uses double backslash instead of single" is incorrect; Java uses single backslash just like other regexes. For example, if you look at your "subject" or "substring" variables in the debugger as the program is running, you would find the string value contains a single backslash. The need for the double-backslash is purely to escape the backslash for a String value (similar to "n", etc), not for the Regex.
– racraman
Nov 20 '18 at 1:54
yes. My bad I just forget it is the case of escape character
– Brij Raj Kishore
Nov 20 '18 at 1:57
add a comment |
There are few meta characters in regex. And some of them which are supported by regex in java are
( ) [ ] { { ^ $ | ? * + . < > - = !
So $ is a indeed meta character here. The meta character conveys special meaning to the regex engine and hence can't be use literally. So in order to use them you have to combine them with escape character which is backslash
So String subject = "\$ABC"
String subString = "\$"
would do. Java uses double backslash instead of single for escape character unlike the other regex engine.
2
"Java uses double backslash instead of single" is incorrect; Java uses single backslash just like other regexes. For example, if you look at your "subject" or "substring" variables in the debugger as the program is running, you would find the string value contains a single backslash. The need for the double-backslash is purely to escape the backslash for a String value (similar to "n", etc), not for the Regex.
– racraman
Nov 20 '18 at 1:54
yes. My bad I just forget it is the case of escape character
– Brij Raj Kishore
Nov 20 '18 at 1:57
add a comment |
There are few meta characters in regex. And some of them which are supported by regex in java are
( ) [ ] { { ^ $ | ? * + . < > - = !
So $ is a indeed meta character here. The meta character conveys special meaning to the regex engine and hence can't be use literally. So in order to use them you have to combine them with escape character which is backslash
So String subject = "\$ABC"
String subString = "\$"
would do. Java uses double backslash instead of single for escape character unlike the other regex engine.
There are few meta characters in regex. And some of them which are supported by regex in java are
( ) [ ] { { ^ $ | ? * + . < > - = !
So $ is a indeed meta character here. The meta character conveys special meaning to the regex engine and hence can't be use literally. So in order to use them you have to combine them with escape character which is backslash
So String subject = "\$ABC"
String subString = "\$"
would do. Java uses double backslash instead of single for escape character unlike the other regex engine.
edited Nov 20 '18 at 1:57
answered Nov 20 '18 at 1:45
Brij Raj KishoreBrij Raj Kishore
1,0201418
1,0201418
2
"Java uses double backslash instead of single" is incorrect; Java uses single backslash just like other regexes. For example, if you look at your "subject" or "substring" variables in the debugger as the program is running, you would find the string value contains a single backslash. The need for the double-backslash is purely to escape the backslash for a String value (similar to "n", etc), not for the Regex.
– racraman
Nov 20 '18 at 1:54
yes. My bad I just forget it is the case of escape character
– Brij Raj Kishore
Nov 20 '18 at 1:57
add a comment |
2
"Java uses double backslash instead of single" is incorrect; Java uses single backslash just like other regexes. For example, if you look at your "subject" or "substring" variables in the debugger as the program is running, you would find the string value contains a single backslash. The need for the double-backslash is purely to escape the backslash for a String value (similar to "n", etc), not for the Regex.
– racraman
Nov 20 '18 at 1:54
yes. My bad I just forget it is the case of escape character
– Brij Raj Kishore
Nov 20 '18 at 1:57
2
2
"Java uses double backslash instead of single" is incorrect; Java uses single backslash just like other regexes. For example, if you look at your "subject" or "substring" variables in the debugger as the program is running, you would find the string value contains a single backslash. The need for the double-backslash is purely to escape the backslash for a String value (similar to "n", etc), not for the Regex.
– racraman
Nov 20 '18 at 1:54
"Java uses double backslash instead of single" is incorrect; Java uses single backslash just like other regexes. For example, if you look at your "subject" or "substring" variables in the debugger as the program is running, you would find the string value contains a single backslash. The need for the double-backslash is purely to escape the backslash for a String value (similar to "n", etc), not for the Regex.
– racraman
Nov 20 '18 at 1:54
yes. My bad I just forget it is the case of escape character
– Brij Raj Kishore
Nov 20 '18 at 1:57
yes. My bad I just forget it is the case of escape character
– Brij Raj Kishore
Nov 20 '18 at 1:57
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385004%2fusing-pattern-and-matcher-to-search-for-special-characters-example%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown