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

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현