how to solve the error of `unknown escape sequence (and 2 more errors)`
I m trying to validate the image url using golang code but there is error in regular expression I'm showing my regular expression in this question:-
var validation = regexp.MustCompile("(http(s?):)|([/|.|w|s])*.(?:jpg|gif|png)")
Error:-
unknown escape sequence (and 2 more errors)
play link
|
show 1 more comment
I m trying to validate the image url using golang code but there is error in regular expression I'm showing my regular expression in this question:-
var validation = regexp.MustCompile("(http(s?):)|([/|.|w|s])*.(?:jpg|gif|png)")
Error:-
unknown escape sequence (and 2 more errors)
play link
I'd suggest using backtics when defining regex so that you won't have to escape ""
– ssemilla
Nov 17 '18 at 9:51
@ssemilla can you tell me what corrections
– stack
Nov 17 '18 at 9:52
Replace " with `. I didn't actually review your regex but people usually make mistakes when they mean "\" rather than "". Using backtics instead makes it very clear what we mean since nothing is escaped.
– ssemilla
Nov 17 '18 at 9:52
@ssemilla By using ` instead of " it runs but not working properly it prints nothing
– stack
Nov 17 '18 at 9:55
Why are you using!validation.MatchString()instead of justvalidation.MatchString()?
– ssemilla
Nov 17 '18 at 9:58
|
show 1 more comment
I m trying to validate the image url using golang code but there is error in regular expression I'm showing my regular expression in this question:-
var validation = regexp.MustCompile("(http(s?):)|([/|.|w|s])*.(?:jpg|gif|png)")
Error:-
unknown escape sequence (and 2 more errors)
play link
I m trying to validate the image url using golang code but there is error in regular expression I'm showing my regular expression in this question:-
var validation = regexp.MustCompile("(http(s?):)|([/|.|w|s])*.(?:jpg|gif|png)")
Error:-
unknown escape sequence (and 2 more errors)
play link
asked Nov 17 '18 at 9:45
stack
105
105
I'd suggest using backtics when defining regex so that you won't have to escape ""
– ssemilla
Nov 17 '18 at 9:51
@ssemilla can you tell me what corrections
– stack
Nov 17 '18 at 9:52
Replace " with `. I didn't actually review your regex but people usually make mistakes when they mean "\" rather than "". Using backtics instead makes it very clear what we mean since nothing is escaped.
– ssemilla
Nov 17 '18 at 9:52
@ssemilla By using ` instead of " it runs but not working properly it prints nothing
– stack
Nov 17 '18 at 9:55
Why are you using!validation.MatchString()instead of justvalidation.MatchString()?
– ssemilla
Nov 17 '18 at 9:58
|
show 1 more comment
I'd suggest using backtics when defining regex so that you won't have to escape ""
– ssemilla
Nov 17 '18 at 9:51
@ssemilla can you tell me what corrections
– stack
Nov 17 '18 at 9:52
Replace " with `. I didn't actually review your regex but people usually make mistakes when they mean "\" rather than "". Using backtics instead makes it very clear what we mean since nothing is escaped.
– ssemilla
Nov 17 '18 at 9:52
@ssemilla By using ` instead of " it runs but not working properly it prints nothing
– stack
Nov 17 '18 at 9:55
Why are you using!validation.MatchString()instead of justvalidation.MatchString()?
– ssemilla
Nov 17 '18 at 9:58
I'd suggest using backtics when defining regex so that you won't have to escape ""
– ssemilla
Nov 17 '18 at 9:51
I'd suggest using backtics when defining regex so that you won't have to escape ""
– ssemilla
Nov 17 '18 at 9:51
@ssemilla can you tell me what corrections
– stack
Nov 17 '18 at 9:52
@ssemilla can you tell me what corrections
– stack
Nov 17 '18 at 9:52
Replace " with `. I didn't actually review your regex but people usually make mistakes when they mean "\" rather than "". Using backtics instead makes it very clear what we mean since nothing is escaped.
– ssemilla
Nov 17 '18 at 9:52
Replace " with `. I didn't actually review your regex but people usually make mistakes when they mean "\" rather than "". Using backtics instead makes it very clear what we mean since nothing is escaped.
– ssemilla
Nov 17 '18 at 9:52
@ssemilla By using ` instead of " it runs but not working properly it prints nothing
– stack
Nov 17 '18 at 9:55
@ssemilla By using ` instead of " it runs but not working properly it prints nothing
– stack
Nov 17 '18 at 9:55
Why are you using
!validation.MatchString() instead of just validation.MatchString()?– ssemilla
Nov 17 '18 at 9:58
Why are you using
!validation.MatchString() instead of just validation.MatchString()?– ssemilla
Nov 17 '18 at 9:58
|
show 1 more comment
1 Answer
1
active
oldest
votes
. is an invalid escape sequence. I would suggest you use backticks when defining regular expressions. e.g.
regexp.MustCompile(`^https?://.*.(jpg|gif|png)$`) // this will just check if the url ends with jpg,gif,png
If you are not using the capture groups, this is a simpler approach. However when parsing or validating URLs, use url.Parse() which provides better validation.
This might be a more proper regex for the URL:^https?://(?:[a-z0-9-]+.)+[a-z]{2,6}(?:/[^/#?]+)+.(?:jpg|gif|png)$which was taken from here: stackoverflow.com/questions/169625/…
– ssemilla
Nov 17 '18 at 10:43
if I used your code given regexp it was also correct?'
– stack
Nov 17 '18 at 11:44
It won't validate invalid URL characters. I suggest you use^https?://(?:[a-z0-9-]+.)+[a-z]{2,6}(?:/[^/#?]+)+.(?:jpg|gif|png)$if you really need a regex.
– ssemilla
Nov 17 '18 at 11:46
Ohh okay thanks for the nice answer :) @ssemilla
– stack
Nov 17 '18 at 12:00
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%2f53350013%2fhow-to-solve-the-error-of-unknown-escape-sequence-and-2-more-errors%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
. is an invalid escape sequence. I would suggest you use backticks when defining regular expressions. e.g.
regexp.MustCompile(`^https?://.*.(jpg|gif|png)$`) // this will just check if the url ends with jpg,gif,png
If you are not using the capture groups, this is a simpler approach. However when parsing or validating URLs, use url.Parse() which provides better validation.
This might be a more proper regex for the URL:^https?://(?:[a-z0-9-]+.)+[a-z]{2,6}(?:/[^/#?]+)+.(?:jpg|gif|png)$which was taken from here: stackoverflow.com/questions/169625/…
– ssemilla
Nov 17 '18 at 10:43
if I used your code given regexp it was also correct?'
– stack
Nov 17 '18 at 11:44
It won't validate invalid URL characters. I suggest you use^https?://(?:[a-z0-9-]+.)+[a-z]{2,6}(?:/[^/#?]+)+.(?:jpg|gif|png)$if you really need a regex.
– ssemilla
Nov 17 '18 at 11:46
Ohh okay thanks for the nice answer :) @ssemilla
– stack
Nov 17 '18 at 12:00
add a comment |
. is an invalid escape sequence. I would suggest you use backticks when defining regular expressions. e.g.
regexp.MustCompile(`^https?://.*.(jpg|gif|png)$`) // this will just check if the url ends with jpg,gif,png
If you are not using the capture groups, this is a simpler approach. However when parsing or validating URLs, use url.Parse() which provides better validation.
This might be a more proper regex for the URL:^https?://(?:[a-z0-9-]+.)+[a-z]{2,6}(?:/[^/#?]+)+.(?:jpg|gif|png)$which was taken from here: stackoverflow.com/questions/169625/…
– ssemilla
Nov 17 '18 at 10:43
if I used your code given regexp it was also correct?'
– stack
Nov 17 '18 at 11:44
It won't validate invalid URL characters. I suggest you use^https?://(?:[a-z0-9-]+.)+[a-z]{2,6}(?:/[^/#?]+)+.(?:jpg|gif|png)$if you really need a regex.
– ssemilla
Nov 17 '18 at 11:46
Ohh okay thanks for the nice answer :) @ssemilla
– stack
Nov 17 '18 at 12:00
add a comment |
. is an invalid escape sequence. I would suggest you use backticks when defining regular expressions. e.g.
regexp.MustCompile(`^https?://.*.(jpg|gif|png)$`) // this will just check if the url ends with jpg,gif,png
If you are not using the capture groups, this is a simpler approach. However when parsing or validating URLs, use url.Parse() which provides better validation.
. is an invalid escape sequence. I would suggest you use backticks when defining regular expressions. e.g.
regexp.MustCompile(`^https?://.*.(jpg|gif|png)$`) // this will just check if the url ends with jpg,gif,png
If you are not using the capture groups, this is a simpler approach. However when parsing or validating URLs, use url.Parse() which provides better validation.
edited Nov 17 '18 at 10:38
answered Nov 17 '18 at 10:23
ssemilla
3,077424
3,077424
This might be a more proper regex for the URL:^https?://(?:[a-z0-9-]+.)+[a-z]{2,6}(?:/[^/#?]+)+.(?:jpg|gif|png)$which was taken from here: stackoverflow.com/questions/169625/…
– ssemilla
Nov 17 '18 at 10:43
if I used your code given regexp it was also correct?'
– stack
Nov 17 '18 at 11:44
It won't validate invalid URL characters. I suggest you use^https?://(?:[a-z0-9-]+.)+[a-z]{2,6}(?:/[^/#?]+)+.(?:jpg|gif|png)$if you really need a regex.
– ssemilla
Nov 17 '18 at 11:46
Ohh okay thanks for the nice answer :) @ssemilla
– stack
Nov 17 '18 at 12:00
add a comment |
This might be a more proper regex for the URL:^https?://(?:[a-z0-9-]+.)+[a-z]{2,6}(?:/[^/#?]+)+.(?:jpg|gif|png)$which was taken from here: stackoverflow.com/questions/169625/…
– ssemilla
Nov 17 '18 at 10:43
if I used your code given regexp it was also correct?'
– stack
Nov 17 '18 at 11:44
It won't validate invalid URL characters. I suggest you use^https?://(?:[a-z0-9-]+.)+[a-z]{2,6}(?:/[^/#?]+)+.(?:jpg|gif|png)$if you really need a regex.
– ssemilla
Nov 17 '18 at 11:46
Ohh okay thanks for the nice answer :) @ssemilla
– stack
Nov 17 '18 at 12:00
This might be a more proper regex for the URL:
^https?://(?:[a-z0-9-]+.)+[a-z]{2,6}(?:/[^/#?]+)+.(?:jpg|gif|png)$ which was taken from here: stackoverflow.com/questions/169625/…– ssemilla
Nov 17 '18 at 10:43
This might be a more proper regex for the URL:
^https?://(?:[a-z0-9-]+.)+[a-z]{2,6}(?:/[^/#?]+)+.(?:jpg|gif|png)$ which was taken from here: stackoverflow.com/questions/169625/…– ssemilla
Nov 17 '18 at 10:43
if I used your code given regexp it was also correct?'
– stack
Nov 17 '18 at 11:44
if I used your code given regexp it was also correct?'
– stack
Nov 17 '18 at 11:44
It won't validate invalid URL characters. I suggest you use
^https?://(?:[a-z0-9-]+.)+[a-z]{2,6}(?:/[^/#?]+)+.(?:jpg|gif|png)$ if you really need a regex.– ssemilla
Nov 17 '18 at 11:46
It won't validate invalid URL characters. I suggest you use
^https?://(?:[a-z0-9-]+.)+[a-z]{2,6}(?:/[^/#?]+)+.(?:jpg|gif|png)$ if you really need a regex.– ssemilla
Nov 17 '18 at 11:46
Ohh okay thanks for the nice answer :) @ssemilla
– stack
Nov 17 '18 at 12:00
Ohh okay thanks for the nice answer :) @ssemilla
– stack
Nov 17 '18 at 12:00
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53350013%2fhow-to-solve-the-error-of-unknown-escape-sequence-and-2-more-errors%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
I'd suggest using backtics when defining regex so that you won't have to escape ""
– ssemilla
Nov 17 '18 at 9:51
@ssemilla can you tell me what corrections
– stack
Nov 17 '18 at 9:52
Replace " with `. I didn't actually review your regex but people usually make mistakes when they mean "\" rather than "". Using backtics instead makes it very clear what we mean since nothing is escaped.
– ssemilla
Nov 17 '18 at 9:52
@ssemilla By using ` instead of " it runs but not working properly it prints nothing
– stack
Nov 17 '18 at 9:55
Why are you using
!validation.MatchString()instead of justvalidation.MatchString()?– ssemilla
Nov 17 '18 at 9:58