Forcing regex to ignore detection depending on prepositionA comprehensive regex for phone number validationIs there a regular expression to detect a valid regular expression?How to negate specific word in regex?RegEx match open tags except XHTML self-contained tagsJS Regex, how to replace the captured groups only?Replace only some groups with RegexA regex not working with grepRegEx doesn't work with .NET, but does with other RegEx implementationsRegex pattern to detect a link but not an imageSwift extract regex matches

What are Elsa's reasons for selecting the Holy Grail on behalf of Donovan?

Justifying Affordable Bespoke Spaceships

Helping ease my back pain by studying 13 hours everyday , even weekends

Non-misogynistic way to say “asshole”?

What happened to Hopper's girlfriend in season one?

Is the specular reflection on a polished gold sphere white or gold in colour?

King or Queen-Which piece is which?

Greeting with "Ho"

macOS: How to take a picture from camera after 1 minute

"Correct me if I'm wrong"

Why isn't my calculation that we should be able to see the sun well beyond the observable universe valid?

Prisoner on alien planet escapes by making up a story about ghost companions and wins the war

Can you use one creature for both convoke and delve for Hogaak?

Am I legally required to provide a (GPL licensed) source code even after a project is abandoned?

Is there any proof that high saturation and contrast makes a picture more appealing in social media?

Are there examples of rowers who also fought?

FD Battery Stations... How Do You Log?

What is the oldest commercial MS-DOS program that can run on modern versions of Windows without third-party software?

Mathematically modelling RC circuit with a linear input

Umlaut character order when sorting

Can Hunter's Mark be moved after Silence has been cast on a character?

What does it cost to buy a tavern?

What is the most suitable position for a bishop here?

Dmesg full of I/O errors, smart ok, four disks affected



Forcing regex to ignore detection depending on preposition


A comprehensive regex for phone number validationIs there a regular expression to detect a valid regular expression?How to negate specific word in regex?RegEx match open tags except XHTML self-contained tagsJS Regex, how to replace the captured groups only?Replace only some groups with RegexA regex not working with grepRegEx doesn't work with .NET, but does with other RegEx implementationsRegex pattern to detect a link but not an imageSwift extract regex matches






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm trying to build a regex, which will detect usernames mentioned in a string. The usernames can look like "username", "username[0-9]", "adm-username", "adm-username[0-9]".



As of now, I have this: b(adm-)0,1username[0-9]0,1b (link: https://regexr.com/4at34)



The problem is with adm-. If the preposition is aadm-username, the regex still detects 'username', I want it to fail. Any tips how to do that?



Thanks










share|improve this question






















  • You want to match any username only with adm- prefix or with no prefix?

    – Michał Turczyn
    Mar 25 at 6:57

















1















I'm trying to build a regex, which will detect usernames mentioned in a string. The usernames can look like "username", "username[0-9]", "adm-username", "adm-username[0-9]".



As of now, I have this: b(adm-)0,1username[0-9]0,1b (link: https://regexr.com/4at34)



The problem is with adm-. If the preposition is aadm-username, the regex still detects 'username', I want it to fail. Any tips how to do that?



Thanks










share|improve this question






















  • You want to match any username only with adm- prefix or with no prefix?

    – Michał Turczyn
    Mar 25 at 6:57













1












1








1








I'm trying to build a regex, which will detect usernames mentioned in a string. The usernames can look like "username", "username[0-9]", "adm-username", "adm-username[0-9]".



As of now, I have this: b(adm-)0,1username[0-9]0,1b (link: https://regexr.com/4at34)



The problem is with adm-. If the preposition is aadm-username, the regex still detects 'username', I want it to fail. Any tips how to do that?



Thanks










share|improve this question














I'm trying to build a regex, which will detect usernames mentioned in a string. The usernames can look like "username", "username[0-9]", "adm-username", "adm-username[0-9]".



As of now, I have this: b(adm-)0,1username[0-9]0,1b (link: https://regexr.com/4at34)



The problem is with adm-. If the preposition is aadm-username, the regex still detects 'username', I want it to fail. Any tips how to do that?



Thanks







regex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 at 6:48









JliskJlisk

586




586












  • You want to match any username only with adm- prefix or with no prefix?

    – Michał Turczyn
    Mar 25 at 6:57

















  • You want to match any username only with adm- prefix or with no prefix?

    – Michał Turczyn
    Mar 25 at 6:57
















You want to match any username only with adm- prefix or with no prefix?

– Michał Turczyn
Mar 25 at 6:57





You want to match any username only with adm- prefix or with no prefix?

– Michał Turczyn
Mar 25 at 6:57












1 Answer
1






active

oldest

votes


















1














You could replace b by [w-] in your case.



Also, don't match the boundaries.



And finally, don't match intermediate groups, make a single big group for your matches.



Demo



(?<![w-])((?:adm-)0,1username[0-9]0,1)(?![w-])



[v] username 
[v] username2
[v] adm-username
[v] adm-username2
[x] aadm-username
[x] aadm-username2


Explanation



(?<![w-]) # negative lookbehind, only match if no word character or hyphen is present
(
(?:adm-)0,1 # non-matching group containing adm- literally once or none, will be matched in the greater group
username[0-9]0,1 # literally matching username and a digit, once or none
)
(?![w-]) # negative lookahead, only match if no word character or hyphen is present





share|improve this answer

























  • Would you mind to 'dissect' the regex with me? Just to see if I understand it correctly. (?<![w-]) -- must not have a word ending with - in front of it. ((?:adm-)0,1 -- checks if the group contains adm-? Not sure about this part. username[0-9]0,1) -- matching the username itself. (?![w-]) -- must not have a word behind itself.

    – Jlisk
    Mar 25 at 7:33







  • 1





    @Jlisk Sure, I'll do it later within the day if you don't mind

    – Yassin Hajaj
    Mar 25 at 8:25






  • 1





    @Jlisk I added a little bit of explanation

    – Yassin Hajaj
    Mar 25 at 12:16












  • Thank you for expanding the answer. About not matching the boundaries, I take it it is better to use lookahead/lookbehind to detect end of the word?

    – Jlisk
    Mar 26 at 5:32











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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332499%2fforcing-regex-to-ignore-detection-depending-on-preposition%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









1














You could replace b by [w-] in your case.



Also, don't match the boundaries.



And finally, don't match intermediate groups, make a single big group for your matches.



Demo



(?<![w-])((?:adm-)0,1username[0-9]0,1)(?![w-])



[v] username 
[v] username2
[v] adm-username
[v] adm-username2
[x] aadm-username
[x] aadm-username2


Explanation



(?<![w-]) # negative lookbehind, only match if no word character or hyphen is present
(
(?:adm-)0,1 # non-matching group containing adm- literally once or none, will be matched in the greater group
username[0-9]0,1 # literally matching username and a digit, once or none
)
(?![w-]) # negative lookahead, only match if no word character or hyphen is present





share|improve this answer

























  • Would you mind to 'dissect' the regex with me? Just to see if I understand it correctly. (?<![w-]) -- must not have a word ending with - in front of it. ((?:adm-)0,1 -- checks if the group contains adm-? Not sure about this part. username[0-9]0,1) -- matching the username itself. (?![w-]) -- must not have a word behind itself.

    – Jlisk
    Mar 25 at 7:33







  • 1





    @Jlisk Sure, I'll do it later within the day if you don't mind

    – Yassin Hajaj
    Mar 25 at 8:25






  • 1





    @Jlisk I added a little bit of explanation

    – Yassin Hajaj
    Mar 25 at 12:16












  • Thank you for expanding the answer. About not matching the boundaries, I take it it is better to use lookahead/lookbehind to detect end of the word?

    – Jlisk
    Mar 26 at 5:32















1














You could replace b by [w-] in your case.



Also, don't match the boundaries.



And finally, don't match intermediate groups, make a single big group for your matches.



Demo



(?<![w-])((?:adm-)0,1username[0-9]0,1)(?![w-])



[v] username 
[v] username2
[v] adm-username
[v] adm-username2
[x] aadm-username
[x] aadm-username2


Explanation



(?<![w-]) # negative lookbehind, only match if no word character or hyphen is present
(
(?:adm-)0,1 # non-matching group containing adm- literally once or none, will be matched in the greater group
username[0-9]0,1 # literally matching username and a digit, once or none
)
(?![w-]) # negative lookahead, only match if no word character or hyphen is present





share|improve this answer

























  • Would you mind to 'dissect' the regex with me? Just to see if I understand it correctly. (?<![w-]) -- must not have a word ending with - in front of it. ((?:adm-)0,1 -- checks if the group contains adm-? Not sure about this part. username[0-9]0,1) -- matching the username itself. (?![w-]) -- must not have a word behind itself.

    – Jlisk
    Mar 25 at 7:33







  • 1





    @Jlisk Sure, I'll do it later within the day if you don't mind

    – Yassin Hajaj
    Mar 25 at 8:25






  • 1





    @Jlisk I added a little bit of explanation

    – Yassin Hajaj
    Mar 25 at 12:16












  • Thank you for expanding the answer. About not matching the boundaries, I take it it is better to use lookahead/lookbehind to detect end of the word?

    – Jlisk
    Mar 26 at 5:32













1












1








1







You could replace b by [w-] in your case.



Also, don't match the boundaries.



And finally, don't match intermediate groups, make a single big group for your matches.



Demo



(?<![w-])((?:adm-)0,1username[0-9]0,1)(?![w-])



[v] username 
[v] username2
[v] adm-username
[v] adm-username2
[x] aadm-username
[x] aadm-username2


Explanation



(?<![w-]) # negative lookbehind, only match if no word character or hyphen is present
(
(?:adm-)0,1 # non-matching group containing adm- literally once or none, will be matched in the greater group
username[0-9]0,1 # literally matching username and a digit, once or none
)
(?![w-]) # negative lookahead, only match if no word character or hyphen is present





share|improve this answer















You could replace b by [w-] in your case.



Also, don't match the boundaries.



And finally, don't match intermediate groups, make a single big group for your matches.



Demo



(?<![w-])((?:adm-)0,1username[0-9]0,1)(?![w-])



[v] username 
[v] username2
[v] adm-username
[v] adm-username2
[x] aadm-username
[x] aadm-username2


Explanation



(?<![w-]) # negative lookbehind, only match if no word character or hyphen is present
(
(?:adm-)0,1 # non-matching group containing adm- literally once or none, will be matched in the greater group
username[0-9]0,1 # literally matching username and a digit, once or none
)
(?![w-]) # negative lookahead, only match if no word character or hyphen is present






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 25 at 12:15

























answered Mar 25 at 6:59









Yassin HajajYassin Hajaj

15.1k83062




15.1k83062












  • Would you mind to 'dissect' the regex with me? Just to see if I understand it correctly. (?<![w-]) -- must not have a word ending with - in front of it. ((?:adm-)0,1 -- checks if the group contains adm-? Not sure about this part. username[0-9]0,1) -- matching the username itself. (?![w-]) -- must not have a word behind itself.

    – Jlisk
    Mar 25 at 7:33







  • 1





    @Jlisk Sure, I'll do it later within the day if you don't mind

    – Yassin Hajaj
    Mar 25 at 8:25






  • 1





    @Jlisk I added a little bit of explanation

    – Yassin Hajaj
    Mar 25 at 12:16












  • Thank you for expanding the answer. About not matching the boundaries, I take it it is better to use lookahead/lookbehind to detect end of the word?

    – Jlisk
    Mar 26 at 5:32

















  • Would you mind to 'dissect' the regex with me? Just to see if I understand it correctly. (?<![w-]) -- must not have a word ending with - in front of it. ((?:adm-)0,1 -- checks if the group contains adm-? Not sure about this part. username[0-9]0,1) -- matching the username itself. (?![w-]) -- must not have a word behind itself.

    – Jlisk
    Mar 25 at 7:33







  • 1





    @Jlisk Sure, I'll do it later within the day if you don't mind

    – Yassin Hajaj
    Mar 25 at 8:25






  • 1





    @Jlisk I added a little bit of explanation

    – Yassin Hajaj
    Mar 25 at 12:16












  • Thank you for expanding the answer. About not matching the boundaries, I take it it is better to use lookahead/lookbehind to detect end of the word?

    – Jlisk
    Mar 26 at 5:32
















Would you mind to 'dissect' the regex with me? Just to see if I understand it correctly. (?<![w-]) -- must not have a word ending with - in front of it. ((?:adm-)0,1 -- checks if the group contains adm-? Not sure about this part. username[0-9]0,1) -- matching the username itself. (?![w-]) -- must not have a word behind itself.

– Jlisk
Mar 25 at 7:33






Would you mind to 'dissect' the regex with me? Just to see if I understand it correctly. (?<![w-]) -- must not have a word ending with - in front of it. ((?:adm-)0,1 -- checks if the group contains adm-? Not sure about this part. username[0-9]0,1) -- matching the username itself. (?![w-]) -- must not have a word behind itself.

– Jlisk
Mar 25 at 7:33





1




1





@Jlisk Sure, I'll do it later within the day if you don't mind

– Yassin Hajaj
Mar 25 at 8:25





@Jlisk Sure, I'll do it later within the day if you don't mind

– Yassin Hajaj
Mar 25 at 8:25




1




1





@Jlisk I added a little bit of explanation

– Yassin Hajaj
Mar 25 at 12:16






@Jlisk I added a little bit of explanation

– Yassin Hajaj
Mar 25 at 12:16














Thank you for expanding the answer. About not matching the boundaries, I take it it is better to use lookahead/lookbehind to detect end of the word?

– Jlisk
Mar 26 at 5:32





Thank you for expanding the answer. About not matching the boundaries, I take it it is better to use lookahead/lookbehind to detect end of the word?

– Jlisk
Mar 26 at 5:32



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332499%2fforcing-regex-to-ignore-detection-depending-on-preposition%23new-answer', 'question_page');

);

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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript