C
C#2y ago
Chris TCC

✅ help with regex

I'm trying to learn regex, have watched multiple tutorials and checked multiple websites, but can't fully grasp how to use it for my specific situation. I want to find and retrieve a certain part out of a string: the word that follows an @. currently I have this: ^@[a-zA-Z0-9]+$ Here are some example cases: word @something -> true @something -> true word@something -> false I'd like to be able to retrieve the @something from those examples, whilst ignoring the rest. Would my regex work? I've tried it with this website (https://regexr.com) but it doesn't seem to work...
RegExr
RegExr: Learn, Build, & Test RegEx
RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).
26 Replies
mtreit
mtreit2y ago
Your use of ^ is a problem It means the string must start with an @
Chris TCC
Chris TCCOP2y ago
Oh. I just thought it had to all start with ^
ChucklesTheBeard
^ means "the start of a line" similarly $ means "the end of a line" it's useful if you're looking for say, a particular kind of message in a huge log file also, take a look at \B
Chris TCC
Chris TCCOP2y ago
Is there any site that lists all the filters? The Microsoft site seems to be missing some
mtreit
mtreit2y ago
Regular Expression Language - Quick Reference
In this quick reference, learn to use regular expression patterns to match input text. A pattern has one or more character literals, operators, or constructs.
Chris TCC
Chris TCCOP2y ago
Oh damn that's a good site I didn't find that one I was using the regex page
mtreit
mtreit2y ago
I think that has a link to the quick reference Anyway, if you really want to learn regex the book Mastering Regular Expressions is the bible
Chris TCC
Chris TCCOP2y ago
Huh. Guess I'm blind then. Alright I'll use the quick reference and try to make a new filter Then I'll get back to here Thanks mates Update: I got this, but I've got one last thing to solve. @[a-zA-Z0-9_]{4,25} it checks for the thing after the @, but I still need to ensure that there is nothing in front of it. can I do [empty space OR start of string]? is there an OR expression that I can use? (I think it might be the | constructor, so maybe (/s|^)@[a-zA-Z0-9_]{4,25}? also, how would I extract that from the string? since regex only returns a bool
MODiX
MODiX2y ago
ChucklesTheBeard#6079
also, take a look at \B
Quoted by
<@!229282705374838794> from #help with regex (click here)
React with ❌ to remove this embed.
ChucklesTheBeard
var r = new Regex(@"pattern");
var extracted = r.Match(inputString).Value
var r = new Regex(@"pattern");
var extracted = r.Match(inputString).Value
for one. .Matches() for multiple
Chris TCC
Chris TCCOP2y ago
ah alright, thanks
Chris TCC
Chris TCCOP2y ago
I don't fully get this
Chris TCC
Chris TCCOP2y ago
from what I understand, it means that there has to be a letter before that?
ChucklesTheBeard
play with it
Chris TCC
Chris TCCOP2y ago
alright
ChucklesTheBeard
If you need to detect an actual space, I think you'll need to use a capture group instead so as to avoid including that space in the match Match().Groups[0].Value or something similar
Chris TCC
Chris TCCOP2y ago
alright nah I think my method or the \B method works still don't fully understand what it does but from my random testing it seems to work
ChucklesTheBeard
test against foo@@bar if that can appear in your input
Chris TCC
Chris TCCOP2y ago
not triggered, as it's supposed to
ChucklesTheBeard
neat
Chris TCC
Chris TCCOP2y ago
I'm gonna try and give the code a test instead of that online site but my software is screaming at me for some reason
Chris TCC
Chris TCCOP2y ago
Chris TCC
Chris TCCOP2y ago
I'll need to figure that one out
ChucklesTheBeard
I find the mute button helps! 🙂
Chris TCC
Chris TCCOP2y ago
I prefer the volume wheel on my headphones
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server