Wildcards in RegEx Haystack (target)?
I want to search for a shorter string in a longer string. Can the longer string (haystack/target) be setup with wildcards?
e.g.) I want to look for 1234 in a string 1234578. Sometimes the long string will be 1@345678 or 12@45678 due to I/O errors. How do I setup the RegEx to match 1234 to 12345678 and any location of the @ symbol anywhere in the long string?
Reason: I have a scanner and it sometimes does not read one character correctly. It replaces that character with a @ symbol. How do I setup my RegEx so that I can have it ignore that @ character and match it?
regex
add a comment |
I want to search for a shorter string in a longer string. Can the longer string (haystack/target) be setup with wildcards?
e.g.) I want to look for 1234 in a string 1234578. Sometimes the long string will be 1@345678 or 12@45678 due to I/O errors. How do I setup the RegEx to match 1234 to 12345678 and any location of the @ symbol anywhere in the long string?
Reason: I have a scanner and it sometimes does not read one character correctly. It replaces that character with a @ symbol. How do I setup my RegEx so that I can have it ignore that @ character and match it?
regex
@Toto If I had 1@345678, and I remove it, it becomes 1345678 and will never match 1234. I'm not sure how I can make it work that way.
– Nelson
Nov 12 '18 at 13:57
You're right, I misread ;(
– Toto
Nov 12 '18 at 14:06
add a comment |
I want to search for a shorter string in a longer string. Can the longer string (haystack/target) be setup with wildcards?
e.g.) I want to look for 1234 in a string 1234578. Sometimes the long string will be 1@345678 or 12@45678 due to I/O errors. How do I setup the RegEx to match 1234 to 12345678 and any location of the @ symbol anywhere in the long string?
Reason: I have a scanner and it sometimes does not read one character correctly. It replaces that character with a @ symbol. How do I setup my RegEx so that I can have it ignore that @ character and match it?
regex
I want to search for a shorter string in a longer string. Can the longer string (haystack/target) be setup with wildcards?
e.g.) I want to look for 1234 in a string 1234578. Sometimes the long string will be 1@345678 or 12@45678 due to I/O errors. How do I setup the RegEx to match 1234 to 12345678 and any location of the @ symbol anywhere in the long string?
Reason: I have a scanner and it sometimes does not read one character correctly. It replaces that character with a @ symbol. How do I setup my RegEx so that I can have it ignore that @ character and match it?
regex
regex
asked Nov 12 '18 at 9:57
NelsonNelson
1,159817
1,159817
@Toto If I had 1@345678, and I remove it, it becomes 1345678 and will never match 1234. I'm not sure how I can make it work that way.
– Nelson
Nov 12 '18 at 13:57
You're right, I misread ;(
– Toto
Nov 12 '18 at 14:06
add a comment |
@Toto If I had 1@345678, and I remove it, it becomes 1345678 and will never match 1234. I'm not sure how I can make it work that way.
– Nelson
Nov 12 '18 at 13:57
You're right, I misread ;(
– Toto
Nov 12 '18 at 14:06
@Toto If I had 1@345678, and I remove it, it becomes 1345678 and will never match 1234. I'm not sure how I can make it work that way.
– Nelson
Nov 12 '18 at 13:57
@Toto If I had 1@345678, and I remove it, it becomes 1345678 and will never match 1234. I'm not sure how I can make it work that way.
– Nelson
Nov 12 '18 at 13:57
You're right, I misread ;(
– Toto
Nov 12 '18 at 14:06
You're right, I misread ;(
– Toto
Nov 12 '18 at 14:06
add a comment |
1 Answer
1
active
oldest
votes
Since I have programmatic control of the parsing process, I can programmatically modify the search string.
So instead of /1234/
, it'll end up being /(1|@)(2|@)(3|@)(4|@)/
It's... really ugly, but that's my current solution.
The process isn't particularly difficult. I'll use regex on the search string by using /(.)/gm
and replace it with ($1|@)
so source 1234
becomes (1|@)(2|@)(3|@)(4|@)
and search with that.
That way they can all be @ symbols. If you only want to match 1 @ out of 1-4, you could use an alternation like(?:1234|@234|1@34|12@4|123@)
Demo
– The fourth bird
Nov 12 '18 at 17:31
@Thefourthbird Hmm, you got any idea on how to turn1234
into(?:1234|@234|1@34|12@4|123@)
?
– Nelson
Nov 13 '18 at 1:28
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%2f53259688%2fwildcards-in-regex-haystack-target%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
Since I have programmatic control of the parsing process, I can programmatically modify the search string.
So instead of /1234/
, it'll end up being /(1|@)(2|@)(3|@)(4|@)/
It's... really ugly, but that's my current solution.
The process isn't particularly difficult. I'll use regex on the search string by using /(.)/gm
and replace it with ($1|@)
so source 1234
becomes (1|@)(2|@)(3|@)(4|@)
and search with that.
That way they can all be @ symbols. If you only want to match 1 @ out of 1-4, you could use an alternation like(?:1234|@234|1@34|12@4|123@)
Demo
– The fourth bird
Nov 12 '18 at 17:31
@Thefourthbird Hmm, you got any idea on how to turn1234
into(?:1234|@234|1@34|12@4|123@)
?
– Nelson
Nov 13 '18 at 1:28
add a comment |
Since I have programmatic control of the parsing process, I can programmatically modify the search string.
So instead of /1234/
, it'll end up being /(1|@)(2|@)(3|@)(4|@)/
It's... really ugly, but that's my current solution.
The process isn't particularly difficult. I'll use regex on the search string by using /(.)/gm
and replace it with ($1|@)
so source 1234
becomes (1|@)(2|@)(3|@)(4|@)
and search with that.
That way they can all be @ symbols. If you only want to match 1 @ out of 1-4, you could use an alternation like(?:1234|@234|1@34|12@4|123@)
Demo
– The fourth bird
Nov 12 '18 at 17:31
@Thefourthbird Hmm, you got any idea on how to turn1234
into(?:1234|@234|1@34|12@4|123@)
?
– Nelson
Nov 13 '18 at 1:28
add a comment |
Since I have programmatic control of the parsing process, I can programmatically modify the search string.
So instead of /1234/
, it'll end up being /(1|@)(2|@)(3|@)(4|@)/
It's... really ugly, but that's my current solution.
The process isn't particularly difficult. I'll use regex on the search string by using /(.)/gm
and replace it with ($1|@)
so source 1234
becomes (1|@)(2|@)(3|@)(4|@)
and search with that.
Since I have programmatic control of the parsing process, I can programmatically modify the search string.
So instead of /1234/
, it'll end up being /(1|@)(2|@)(3|@)(4|@)/
It's... really ugly, but that's my current solution.
The process isn't particularly difficult. I'll use regex on the search string by using /(.)/gm
and replace it with ($1|@)
so source 1234
becomes (1|@)(2|@)(3|@)(4|@)
and search with that.
answered Nov 12 '18 at 9:59
NelsonNelson
1,159817
1,159817
That way they can all be @ symbols. If you only want to match 1 @ out of 1-4, you could use an alternation like(?:1234|@234|1@34|12@4|123@)
Demo
– The fourth bird
Nov 12 '18 at 17:31
@Thefourthbird Hmm, you got any idea on how to turn1234
into(?:1234|@234|1@34|12@4|123@)
?
– Nelson
Nov 13 '18 at 1:28
add a comment |
That way they can all be @ symbols. If you only want to match 1 @ out of 1-4, you could use an alternation like(?:1234|@234|1@34|12@4|123@)
Demo
– The fourth bird
Nov 12 '18 at 17:31
@Thefourthbird Hmm, you got any idea on how to turn1234
into(?:1234|@234|1@34|12@4|123@)
?
– Nelson
Nov 13 '18 at 1:28
That way they can all be @ symbols. If you only want to match 1 @ out of 1-4, you could use an alternation like
(?:1234|@234|1@34|12@4|123@)
Demo– The fourth bird
Nov 12 '18 at 17:31
That way they can all be @ symbols. If you only want to match 1 @ out of 1-4, you could use an alternation like
(?:1234|@234|1@34|12@4|123@)
Demo– The fourth bird
Nov 12 '18 at 17:31
@Thefourthbird Hmm, you got any idea on how to turn
1234
into (?:1234|@234|1@34|12@4|123@)
?– Nelson
Nov 13 '18 at 1:28
@Thefourthbird Hmm, you got any idea on how to turn
1234
into (?:1234|@234|1@34|12@4|123@)
?– Nelson
Nov 13 '18 at 1:28
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%2f53259688%2fwildcards-in-regex-haystack-target%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
@Toto If I had 1@345678, and I remove it, it becomes 1345678 and will never match 1234. I'm not sure how I can make it work that way.
– Nelson
Nov 12 '18 at 13:57
You're right, I misread ;(
– Toto
Nov 12 '18 at 14:06