Regex Match a character which is not followed by another specific character
I'm writing a CodeMirror extension for Brackets. To defineSimpleCodeMode I need to do some pattern matching and I'm trying to figure out how to achieve $subject.
e.g.
Match <
of all the html tags
<body>
And ignore html tags which are followed by <%
<% if %>
Note: I only want to get the starting <
of it
If some can help me out it would be a great help. Please do let me know if you need anymore details.
Thanks!
javascript regex
add a comment |
I'm writing a CodeMirror extension for Brackets. To defineSimpleCodeMode I need to do some pattern matching and I'm trying to figure out how to achieve $subject.
e.g.
Match <
of all the html tags
<body>
And ignore html tags which are followed by <%
<% if %>
Note: I only want to get the starting <
of it
If some can help me out it would be a great help. Please do let me know if you need anymore details.
Thanks!
javascript regex
3
using a regex to parse a template string, can be a very bad idea
– Daniel A. White
Aug 10 '16 at 10:57
Hi @DanielA.White this is to write an CodeMode extension.
– Jerad Rutnam
Aug 10 '16 at 10:58
add a comment |
I'm writing a CodeMirror extension for Brackets. To defineSimpleCodeMode I need to do some pattern matching and I'm trying to figure out how to achieve $subject.
e.g.
Match <
of all the html tags
<body>
And ignore html tags which are followed by <%
<% if %>
Note: I only want to get the starting <
of it
If some can help me out it would be a great help. Please do let me know if you need anymore details.
Thanks!
javascript regex
I'm writing a CodeMirror extension for Brackets. To defineSimpleCodeMode I need to do some pattern matching and I'm trying to figure out how to achieve $subject.
e.g.
Match <
of all the html tags
<body>
And ignore html tags which are followed by <%
<% if %>
Note: I only want to get the starting <
of it
If some can help me out it would be a great help. Please do let me know if you need anymore details.
Thanks!
javascript regex
javascript regex
edited Dec 13 '18 at 8:40
Wiktor Stribiżew
325k16146226
325k16146226
asked Aug 10 '16 at 10:56
Jerad RutnamJerad Rutnam
1,0961825
1,0961825
3
using a regex to parse a template string, can be a very bad idea
– Daniel A. White
Aug 10 '16 at 10:57
Hi @DanielA.White this is to write an CodeMode extension.
– Jerad Rutnam
Aug 10 '16 at 10:58
add a comment |
3
using a regex to parse a template string, can be a very bad idea
– Daniel A. White
Aug 10 '16 at 10:57
Hi @DanielA.White this is to write an CodeMode extension.
– Jerad Rutnam
Aug 10 '16 at 10:58
3
3
using a regex to parse a template string, can be a very bad idea
– Daniel A. White
Aug 10 '16 at 10:57
using a regex to parse a template string, can be a very bad idea
– Daniel A. White
Aug 10 '16 at 10:57
Hi @DanielA.White this is to write an CodeMode extension.
– Jerad Rutnam
Aug 10 '16 at 10:58
Hi @DanielA.White this is to write an CodeMode extension.
– Jerad Rutnam
Aug 10 '16 at 10:58
add a comment |
1 Answer
1
active
oldest
votes
While this seems to be a bad idea, I can see two ways of doing it :
1. Searching for <
followed by anything but the %
character, then ignoring it
(<)(?:[^%])
The [^]
sequence allows you to search for anything but the following character.
The (?:)
sequence is for non capturing groups.
2. (Better, if supported) Searching for input not followed by % with a negative lookahead
<(?!%)
The (?!)
sequence succeeds if it doesn't match the following character, but is not captured.
If you also want to do it for %>
, you can just "reverse" the first option :
(?:[^%])(>)
Or you need a negative lookbehind :
(careful here, the lookahead won't work as you need to go backwards)
(?<!%)>
Hi @Baptiste, Thanks for the answer. It almost do what I wanted.How is it possible to ignore the second charater which is after<
?
– Jerad Rutnam
Aug 10 '16 at 11:03
I don't know what tool you are working with, but you could just replace what you get with the$1
variable with most of them.
– Azaghal
Aug 10 '16 at 11:06
Im using regex101.com.
– Jerad Rutnam
Aug 10 '16 at 11:11
@JeradRutnam what you you mean the second character? It's already ignoring the second%
– phuclv
Aug 10 '16 at 11:12
Can we add$1
along with the match(<)[^%]
somehow?
– Jerad Rutnam
Aug 10 '16 at 11:13
|
show 8 more comments
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%2f38871435%2fregex-match-a-character-which-is-not-followed-by-another-specific-character%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
While this seems to be a bad idea, I can see two ways of doing it :
1. Searching for <
followed by anything but the %
character, then ignoring it
(<)(?:[^%])
The [^]
sequence allows you to search for anything but the following character.
The (?:)
sequence is for non capturing groups.
2. (Better, if supported) Searching for input not followed by % with a negative lookahead
<(?!%)
The (?!)
sequence succeeds if it doesn't match the following character, but is not captured.
If you also want to do it for %>
, you can just "reverse" the first option :
(?:[^%])(>)
Or you need a negative lookbehind :
(careful here, the lookahead won't work as you need to go backwards)
(?<!%)>
Hi @Baptiste, Thanks for the answer. It almost do what I wanted.How is it possible to ignore the second charater which is after<
?
– Jerad Rutnam
Aug 10 '16 at 11:03
I don't know what tool you are working with, but you could just replace what you get with the$1
variable with most of them.
– Azaghal
Aug 10 '16 at 11:06
Im using regex101.com.
– Jerad Rutnam
Aug 10 '16 at 11:11
@JeradRutnam what you you mean the second character? It's already ignoring the second%
– phuclv
Aug 10 '16 at 11:12
Can we add$1
along with the match(<)[^%]
somehow?
– Jerad Rutnam
Aug 10 '16 at 11:13
|
show 8 more comments
While this seems to be a bad idea, I can see two ways of doing it :
1. Searching for <
followed by anything but the %
character, then ignoring it
(<)(?:[^%])
The [^]
sequence allows you to search for anything but the following character.
The (?:)
sequence is for non capturing groups.
2. (Better, if supported) Searching for input not followed by % with a negative lookahead
<(?!%)
The (?!)
sequence succeeds if it doesn't match the following character, but is not captured.
If you also want to do it for %>
, you can just "reverse" the first option :
(?:[^%])(>)
Or you need a negative lookbehind :
(careful here, the lookahead won't work as you need to go backwards)
(?<!%)>
Hi @Baptiste, Thanks for the answer. It almost do what I wanted.How is it possible to ignore the second charater which is after<
?
– Jerad Rutnam
Aug 10 '16 at 11:03
I don't know what tool you are working with, but you could just replace what you get with the$1
variable with most of them.
– Azaghal
Aug 10 '16 at 11:06
Im using regex101.com.
– Jerad Rutnam
Aug 10 '16 at 11:11
@JeradRutnam what you you mean the second character? It's already ignoring the second%
– phuclv
Aug 10 '16 at 11:12
Can we add$1
along with the match(<)[^%]
somehow?
– Jerad Rutnam
Aug 10 '16 at 11:13
|
show 8 more comments
While this seems to be a bad idea, I can see two ways of doing it :
1. Searching for <
followed by anything but the %
character, then ignoring it
(<)(?:[^%])
The [^]
sequence allows you to search for anything but the following character.
The (?:)
sequence is for non capturing groups.
2. (Better, if supported) Searching for input not followed by % with a negative lookahead
<(?!%)
The (?!)
sequence succeeds if it doesn't match the following character, but is not captured.
If you also want to do it for %>
, you can just "reverse" the first option :
(?:[^%])(>)
Or you need a negative lookbehind :
(careful here, the lookahead won't work as you need to go backwards)
(?<!%)>
While this seems to be a bad idea, I can see two ways of doing it :
1. Searching for <
followed by anything but the %
character, then ignoring it
(<)(?:[^%])
The [^]
sequence allows you to search for anything but the following character.
The (?:)
sequence is for non capturing groups.
2. (Better, if supported) Searching for input not followed by % with a negative lookahead
<(?!%)
The (?!)
sequence succeeds if it doesn't match the following character, but is not captured.
If you also want to do it for %>
, you can just "reverse" the first option :
(?:[^%])(>)
Or you need a negative lookbehind :
(careful here, the lookahead won't work as you need to go backwards)
(?<!%)>
edited Aug 10 '16 at 12:27
answered Aug 10 '16 at 11:01
AzaghalAzaghal
245110
245110
Hi @Baptiste, Thanks for the answer. It almost do what I wanted.How is it possible to ignore the second charater which is after<
?
– Jerad Rutnam
Aug 10 '16 at 11:03
I don't know what tool you are working with, but you could just replace what you get with the$1
variable with most of them.
– Azaghal
Aug 10 '16 at 11:06
Im using regex101.com.
– Jerad Rutnam
Aug 10 '16 at 11:11
@JeradRutnam what you you mean the second character? It's already ignoring the second%
– phuclv
Aug 10 '16 at 11:12
Can we add$1
along with the match(<)[^%]
somehow?
– Jerad Rutnam
Aug 10 '16 at 11:13
|
show 8 more comments
Hi @Baptiste, Thanks for the answer. It almost do what I wanted.How is it possible to ignore the second charater which is after<
?
– Jerad Rutnam
Aug 10 '16 at 11:03
I don't know what tool you are working with, but you could just replace what you get with the$1
variable with most of them.
– Azaghal
Aug 10 '16 at 11:06
Im using regex101.com.
– Jerad Rutnam
Aug 10 '16 at 11:11
@JeradRutnam what you you mean the second character? It's already ignoring the second%
– phuclv
Aug 10 '16 at 11:12
Can we add$1
along with the match(<)[^%]
somehow?
– Jerad Rutnam
Aug 10 '16 at 11:13
Hi @Baptiste, Thanks for the answer. It almost do what I wanted.How is it possible to ignore the second charater which is after
<
?– Jerad Rutnam
Aug 10 '16 at 11:03
Hi @Baptiste, Thanks for the answer. It almost do what I wanted.How is it possible to ignore the second charater which is after
<
?– Jerad Rutnam
Aug 10 '16 at 11:03
I don't know what tool you are working with, but you could just replace what you get with the
$1
variable with most of them.– Azaghal
Aug 10 '16 at 11:06
I don't know what tool you are working with, but you could just replace what you get with the
$1
variable with most of them.– Azaghal
Aug 10 '16 at 11:06
Im using regex101.com.
– Jerad Rutnam
Aug 10 '16 at 11:11
Im using regex101.com.
– Jerad Rutnam
Aug 10 '16 at 11:11
@JeradRutnam what you you mean the second character? It's already ignoring the second
%
– phuclv
Aug 10 '16 at 11:12
@JeradRutnam what you you mean the second character? It's already ignoring the second
%
– phuclv
Aug 10 '16 at 11:12
Can we add
$1
along with the match (<)[^%]
somehow?– Jerad Rutnam
Aug 10 '16 at 11:13
Can we add
$1
along with the match (<)[^%]
somehow?– Jerad Rutnam
Aug 10 '16 at 11:13
|
show 8 more comments
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%2f38871435%2fregex-match-a-character-which-is-not-followed-by-another-specific-character%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
3
using a regex to parse a template string, can be a very bad idea
– Daniel A. White
Aug 10 '16 at 10:57
Hi @DanielA.White this is to write an CodeMode extension.
– Jerad Rutnam
Aug 10 '16 at 10:58