Regex: Replacing the first leftmost occurence or the first rightmost occurence

I need to create a regex that could modify something similar to the following string.

[[12][abad][32][@adb.]

So if I wanted to match with the leftmost occurrence of a, it’d mark the following.

[[12][*a*bad][32][@adb.]

Or if I matched with the rightmost occurrence of @ad, it’d mark the following:

 [[12][abad][32][*@ad*b.]

Ideally, I’d want the regex to be sort of like a function (i.e., given a substring and/or character, match with the first leftmost occurrence, or match with the first rightmost occurrence, of that input). I’d be able to pick any substring, and there’d be some static component in the regex that’s easily modifiable (making it act like a function).

I’ve seen some similar questions that mentioned lazy regex with *? to get the first match (for example, I saw a question that gave [#(.*?)] as its answer), but I wasn’t sure how to integrate the answers given and make the function I mentioned above, that could take any string/character input and find its left-most or right-most occurrence.

Read more here: Source link