C
C#2y ago
mortious

❔ Regex expression to find the last occurrence in a string

Looking for a regex expression to find the last occurrence of any of the following characters ( + - * \ in a given string. I need to get the text following the last occurrence of any of those characters and return a list of auto complete suggestive text from a list of possibilities I have built.
23 Replies
Anton
Anton2y ago
[(+\-*\\]([^(+\-*\\]*)$ something like this should work I'm not sure everything is escaped properly the first group (index 1) is your text
mortious
mortiousOP2y ago
Thanks Anton, could you break it down and explain if you have time? I get the stuff in [] looks for characters what is the stuff in ([]) ? will try it out real quick Also don't quite understand the ^ * and $ what they mean
mortious
mortiousOP2y ago
wow that worked well in the https://regex101.com/ tool
regex101
regex101: build, test, and debug regex
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET.
mortious
mortiousOP2y ago
ok so ^ says at start, * is 0 or more times and ? is at end? so the stuff in the first [] says match this .. and the stuff in () basically says under these conditions? reading the manual on regex but its a bit cryptic to me lol
Anton
Anton2y ago
[] means match what's inside () make a group so you can take them by index later
mortious
mortiousOP2y ago
ahh so group by?
Anton
Anton2y ago
caret within [] inverts it * means 0+ times $ means end of string so that regex matches one of your characters, then 0+ not of your characters, at the end of input i meant match one of the characters inside but [] also has range syntax, so you have to escape symbols that would otherwise make a range, I think e.g. [a-z] matches the range from a to z rather than a, - or z [a\-z] would match one of these 3 i think ah also I should mention
mortious
mortiousOP2y ago
lol so many rules
Anton
Anton2y ago
put the regex string into a raw string literal aka a verbatim string
mortious
mortiousOP2y ago
@"" ?
Anton
Anton2y ago
@"string here"
mortious
mortiousOP2y ago
yeah k
Anton
Anton2y ago
if you don't, you'll have to escape backslashes an extra time
mortious
mortiousOP2y ago
thanks man. it seems to be working for my purpose. I'll pop it into my code and see how it works, then I can move on to provide suggestions 🙂 I am building a list of suggestions based off methods I am reflecting on in a Namespace so when anyone adds functions to that namespace it automatically picks up the new functions and their parameters So I can provide suggestions while people are typing kind of like home brew intellisense lol
Anton
Anton2y ago
why aren't you using a library for this
mortious
mortiousOP2y ago
well
Anton
Anton2y ago
seems like too much work
mortious
mortiousOP2y ago
If I knew of a library i would.. but there are custom elements in the list
Anton
Anton2y ago
Roslyn make it read your assembly definitions or the source code dierctly
mortious
mortiousOP2y ago
so like someone using the software. They create a step for a machine to do something and the intellisense has to pickup on those past activities so it can reference past steps That's pretty cool i'll have to look into it I always hear the name but never used it lol or knew what it was
Anton
Anton2y ago
it's the whole compiler as a library in fact all modern c# compilers are based on that
mortious
mortiousOP2y ago
nice wups my divide by operator was backwards lol i typed \ instead of /
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