Regex: Find strings without character
i build regex expression that matches
2 letters or 2 letters folowed by '/' and next 2 letters for example:
rt bl/ws se gn/wd wk bl/rt
/^(((s+)?[a-zA-Z]{2}(/[a-zA-Z]{2})?)(s+|$))+$/i
and that works without problems.
Next problem what I have is match all "word" not containing '/' character.
and replace all matches by duplicate values separated by '/'. For above example excepted output should be:
rt/rt bl/ws se/se gn/wd wk/wk bl/rt
I tried it some time but without success. Could you help me with that ?
Update 1:
I've started with regex that matches words not containing 'at'
(b((?!(at))w)+b)
Et the and I want to replace matched elements with python like
re.sub(r'(b((?!(at))w)+b)', r'1/1', text)
but first have to find right elements ...
javascript regex regex-negation
|
show 1 more comment
i build regex expression that matches
2 letters or 2 letters folowed by '/' and next 2 letters for example:
rt bl/ws se gn/wd wk bl/rt
/^(((s+)?[a-zA-Z]{2}(/[a-zA-Z]{2})?)(s+|$))+$/i
and that works without problems.
Next problem what I have is match all "word" not containing '/' character.
and replace all matches by duplicate values separated by '/'. For above example excepted output should be:
rt/rt bl/ws se/se gn/wd wk/wk bl/rt
I tried it some time but without success. Could you help me with that ?
Update 1:
I've started with regex that matches words not containing 'at'
(b((?!(at))w)+b)
Et the and I want to replace matched elements with python like
re.sub(r'(b((?!(at))w)+b)', r'1/1', text)
but first have to find right elements ...
javascript regex regex-negation
1
Show us what you tried, it will help identify your problem.
– Wiktor Stribiżew
Nov 20 '18 at 22:11
Also, it would be great if you mentioned the programming language you are using the regex in.
– Wiktor Stribiżew
Nov 20 '18 at 22:15
Your regex is very complicated. You could simplify it. For instance,(s+)?
is the same ass*
. Also have a look atw
andb
.
– Socowi
Nov 20 '18 at 22:16
@Wiktor Stribiżew OP never excepts that work will be made by someone else. Is there ask for help not right place on this forum ?
– walko1234
Nov 20 '18 at 22:53
Then you may usere.sub(r'(?<!S)[^Wd_]+(?!S)', r'g<0>/g<0>', s)
. Orre.sub(r'(?<!S)([^Wd_]+)(?!S)', r'1/1', s)
. To excludeat
word matches, usere.sub(r'(?<!S)(?!at(?!S))([^Wd_]+)(?!S)', r'1/1', s)
– Wiktor Stribiżew
Nov 21 '18 at 0:40
|
show 1 more comment
i build regex expression that matches
2 letters or 2 letters folowed by '/' and next 2 letters for example:
rt bl/ws se gn/wd wk bl/rt
/^(((s+)?[a-zA-Z]{2}(/[a-zA-Z]{2})?)(s+|$))+$/i
and that works without problems.
Next problem what I have is match all "word" not containing '/' character.
and replace all matches by duplicate values separated by '/'. For above example excepted output should be:
rt/rt bl/ws se/se gn/wd wk/wk bl/rt
I tried it some time but without success. Could you help me with that ?
Update 1:
I've started with regex that matches words not containing 'at'
(b((?!(at))w)+b)
Et the and I want to replace matched elements with python like
re.sub(r'(b((?!(at))w)+b)', r'1/1', text)
but first have to find right elements ...
javascript regex regex-negation
i build regex expression that matches
2 letters or 2 letters folowed by '/' and next 2 letters for example:
rt bl/ws se gn/wd wk bl/rt
/^(((s+)?[a-zA-Z]{2}(/[a-zA-Z]{2})?)(s+|$))+$/i
and that works without problems.
Next problem what I have is match all "word" not containing '/' character.
and replace all matches by duplicate values separated by '/'. For above example excepted output should be:
rt/rt bl/ws se/se gn/wd wk/wk bl/rt
I tried it some time but without success. Could you help me with that ?
Update 1:
I've started with regex that matches words not containing 'at'
(b((?!(at))w)+b)
Et the and I want to replace matched elements with python like
re.sub(r'(b((?!(at))w)+b)', r'1/1', text)
but first have to find right elements ...
javascript regex regex-negation
javascript regex regex-negation
edited Nov 22 '18 at 7:57
JohnyL
3,7231924
3,7231924
asked Nov 20 '18 at 22:09
walko1234walko1234
466
466
1
Show us what you tried, it will help identify your problem.
– Wiktor Stribiżew
Nov 20 '18 at 22:11
Also, it would be great if you mentioned the programming language you are using the regex in.
– Wiktor Stribiżew
Nov 20 '18 at 22:15
Your regex is very complicated. You could simplify it. For instance,(s+)?
is the same ass*
. Also have a look atw
andb
.
– Socowi
Nov 20 '18 at 22:16
@Wiktor Stribiżew OP never excepts that work will be made by someone else. Is there ask for help not right place on this forum ?
– walko1234
Nov 20 '18 at 22:53
Then you may usere.sub(r'(?<!S)[^Wd_]+(?!S)', r'g<0>/g<0>', s)
. Orre.sub(r'(?<!S)([^Wd_]+)(?!S)', r'1/1', s)
. To excludeat
word matches, usere.sub(r'(?<!S)(?!at(?!S))([^Wd_]+)(?!S)', r'1/1', s)
– Wiktor Stribiżew
Nov 21 '18 at 0:40
|
show 1 more comment
1
Show us what you tried, it will help identify your problem.
– Wiktor Stribiżew
Nov 20 '18 at 22:11
Also, it would be great if you mentioned the programming language you are using the regex in.
– Wiktor Stribiżew
Nov 20 '18 at 22:15
Your regex is very complicated. You could simplify it. For instance,(s+)?
is the same ass*
. Also have a look atw
andb
.
– Socowi
Nov 20 '18 at 22:16
@Wiktor Stribiżew OP never excepts that work will be made by someone else. Is there ask for help not right place on this forum ?
– walko1234
Nov 20 '18 at 22:53
Then you may usere.sub(r'(?<!S)[^Wd_]+(?!S)', r'g<0>/g<0>', s)
. Orre.sub(r'(?<!S)([^Wd_]+)(?!S)', r'1/1', s)
. To excludeat
word matches, usere.sub(r'(?<!S)(?!at(?!S))([^Wd_]+)(?!S)', r'1/1', s)
– Wiktor Stribiżew
Nov 21 '18 at 0:40
1
1
Show us what you tried, it will help identify your problem.
– Wiktor Stribiżew
Nov 20 '18 at 22:11
Show us what you tried, it will help identify your problem.
– Wiktor Stribiżew
Nov 20 '18 at 22:11
Also, it would be great if you mentioned the programming language you are using the regex in.
– Wiktor Stribiżew
Nov 20 '18 at 22:15
Also, it would be great if you mentioned the programming language you are using the regex in.
– Wiktor Stribiżew
Nov 20 '18 at 22:15
Your regex is very complicated. You could simplify it. For instance,
(s+)?
is the same as s*
. Also have a look at w
and b
.– Socowi
Nov 20 '18 at 22:16
Your regex is very complicated. You could simplify it. For instance,
(s+)?
is the same as s*
. Also have a look at w
and b
.– Socowi
Nov 20 '18 at 22:16
@Wiktor Stribiżew OP never excepts that work will be made by someone else. Is there ask for help not right place on this forum ?
– walko1234
Nov 20 '18 at 22:53
@Wiktor Stribiżew OP never excepts that work will be made by someone else. Is there ask for help not right place on this forum ?
– walko1234
Nov 20 '18 at 22:53
Then you may use
re.sub(r'(?<!S)[^Wd_]+(?!S)', r'g<0>/g<0>', s)
. Or re.sub(r'(?<!S)([^Wd_]+)(?!S)', r'1/1', s)
. To exclude at
word matches, use re.sub(r'(?<!S)(?!at(?!S))([^Wd_]+)(?!S)', r'1/1', s)
– Wiktor Stribiżew
Nov 21 '18 at 0:40
Then you may use
re.sub(r'(?<!S)[^Wd_]+(?!S)', r'g<0>/g<0>', s)
. Or re.sub(r'(?<!S)([^Wd_]+)(?!S)', r'1/1', s)
. To exclude at
word matches, use re.sub(r'(?<!S)(?!at(?!S))([^Wd_]+)(?!S)', r'1/1', s)
– Wiktor Stribiżew
Nov 21 '18 at 0:40
|
show 1 more comment
2 Answers
2
active
oldest
votes
in python you can do something like:
re.sub(r'(?:(?<=^)|(?<= ))(w+)(?= )',r'1/1','rt bl/ws se gn/wd wk bl/rt')
'rt/rt bl/ws se/se gn/wd wk/wk bl/rt'
add a comment |
If you're using a flavour of regex that supports negative lookbehind this regex should find the strings you want to replace:
(?<!/)([A-Za-z]{2}(?!/))
(?<!/)
- makes sure there isn't a /
behind the match([A-Za-z]{2}
looks for two alphabetic characters(?!/))
makes sure there isn't a trailing /
In Python you would use it like this:
print(re.sub(r'(?<!/)([A-Za-z]{2}(?!/))',r'1/1','rt bl/ws se gn/wd wk bl/rt'))
Output:
rt/rt bl/ws se/se gn/wd wk/wk bl/rt
Demo on rextester
In PHP you would use it like so:
$str = 'rt bl/ws se gn/wd wk bl/rt';
echo preg_replace('/(?<!/)([A-Za-z]{2}(?!/))/', '$1/$1', $str);
Output:
rt/rt bl/ws se/se gn/wd wk/wk bl/rt
Demo on 3v4l.org
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%2f53402349%2fregex-find-strings-without-character%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
in python you can do something like:
re.sub(r'(?:(?<=^)|(?<= ))(w+)(?= )',r'1/1','rt bl/ws se gn/wd wk bl/rt')
'rt/rt bl/ws se/se gn/wd wk/wk bl/rt'
add a comment |
in python you can do something like:
re.sub(r'(?:(?<=^)|(?<= ))(w+)(?= )',r'1/1','rt bl/ws se gn/wd wk bl/rt')
'rt/rt bl/ws se/se gn/wd wk/wk bl/rt'
add a comment |
in python you can do something like:
re.sub(r'(?:(?<=^)|(?<= ))(w+)(?= )',r'1/1','rt bl/ws se gn/wd wk bl/rt')
'rt/rt bl/ws se/se gn/wd wk/wk bl/rt'
in python you can do something like:
re.sub(r'(?:(?<=^)|(?<= ))(w+)(?= )',r'1/1','rt bl/ws se gn/wd wk bl/rt')
'rt/rt bl/ws se/se gn/wd wk/wk bl/rt'
answered Nov 20 '18 at 23:17
OnyambuOnyambu
16k1522
16k1522
add a comment |
add a comment |
If you're using a flavour of regex that supports negative lookbehind this regex should find the strings you want to replace:
(?<!/)([A-Za-z]{2}(?!/))
(?<!/)
- makes sure there isn't a /
behind the match([A-Za-z]{2}
looks for two alphabetic characters(?!/))
makes sure there isn't a trailing /
In Python you would use it like this:
print(re.sub(r'(?<!/)([A-Za-z]{2}(?!/))',r'1/1','rt bl/ws se gn/wd wk bl/rt'))
Output:
rt/rt bl/ws se/se gn/wd wk/wk bl/rt
Demo on rextester
In PHP you would use it like so:
$str = 'rt bl/ws se gn/wd wk bl/rt';
echo preg_replace('/(?<!/)([A-Za-z]{2}(?!/))/', '$1/$1', $str);
Output:
rt/rt bl/ws se/se gn/wd wk/wk bl/rt
Demo on 3v4l.org
add a comment |
If you're using a flavour of regex that supports negative lookbehind this regex should find the strings you want to replace:
(?<!/)([A-Za-z]{2}(?!/))
(?<!/)
- makes sure there isn't a /
behind the match([A-Za-z]{2}
looks for two alphabetic characters(?!/))
makes sure there isn't a trailing /
In Python you would use it like this:
print(re.sub(r'(?<!/)([A-Za-z]{2}(?!/))',r'1/1','rt bl/ws se gn/wd wk bl/rt'))
Output:
rt/rt bl/ws se/se gn/wd wk/wk bl/rt
Demo on rextester
In PHP you would use it like so:
$str = 'rt bl/ws se gn/wd wk bl/rt';
echo preg_replace('/(?<!/)([A-Za-z]{2}(?!/))/', '$1/$1', $str);
Output:
rt/rt bl/ws se/se gn/wd wk/wk bl/rt
Demo on 3v4l.org
add a comment |
If you're using a flavour of regex that supports negative lookbehind this regex should find the strings you want to replace:
(?<!/)([A-Za-z]{2}(?!/))
(?<!/)
- makes sure there isn't a /
behind the match([A-Za-z]{2}
looks for two alphabetic characters(?!/))
makes sure there isn't a trailing /
In Python you would use it like this:
print(re.sub(r'(?<!/)([A-Za-z]{2}(?!/))',r'1/1','rt bl/ws se gn/wd wk bl/rt'))
Output:
rt/rt bl/ws se/se gn/wd wk/wk bl/rt
Demo on rextester
In PHP you would use it like so:
$str = 'rt bl/ws se gn/wd wk bl/rt';
echo preg_replace('/(?<!/)([A-Za-z]{2}(?!/))/', '$1/$1', $str);
Output:
rt/rt bl/ws se/se gn/wd wk/wk bl/rt
Demo on 3v4l.org
If you're using a flavour of regex that supports negative lookbehind this regex should find the strings you want to replace:
(?<!/)([A-Za-z]{2}(?!/))
(?<!/)
- makes sure there isn't a /
behind the match([A-Za-z]{2}
looks for two alphabetic characters(?!/))
makes sure there isn't a trailing /
In Python you would use it like this:
print(re.sub(r'(?<!/)([A-Za-z]{2}(?!/))',r'1/1','rt bl/ws se gn/wd wk bl/rt'))
Output:
rt/rt bl/ws se/se gn/wd wk/wk bl/rt
Demo on rextester
In PHP you would use it like so:
$str = 'rt bl/ws se gn/wd wk bl/rt';
echo preg_replace('/(?<!/)([A-Za-z]{2}(?!/))/', '$1/$1', $str);
Output:
rt/rt bl/ws se/se gn/wd wk/wk bl/rt
Demo on 3v4l.org
edited Nov 22 '18 at 7:09
answered Nov 20 '18 at 23:03
NickNick
33.3k132042
33.3k132042
add a comment |
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%2f53402349%2fregex-find-strings-without-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
1
Show us what you tried, it will help identify your problem.
– Wiktor Stribiżew
Nov 20 '18 at 22:11
Also, it would be great if you mentioned the programming language you are using the regex in.
– Wiktor Stribiżew
Nov 20 '18 at 22:15
Your regex is very complicated. You could simplify it. For instance,
(s+)?
is the same ass*
. Also have a look atw
andb
.– Socowi
Nov 20 '18 at 22:16
@Wiktor Stribiżew OP never excepts that work will be made by someone else. Is there ask for help not right place on this forum ?
– walko1234
Nov 20 '18 at 22:53
Then you may use
re.sub(r'(?<!S)[^Wd_]+(?!S)', r'g<0>/g<0>', s)
. Orre.sub(r'(?<!S)([^Wd_]+)(?!S)', r'1/1', s)
. To excludeat
word matches, usere.sub(r'(?<!S)(?!at(?!S))([^Wd_]+)(?!S)', r'1/1', s)
– Wiktor Stribiżew
Nov 21 '18 at 0:40