Comments on: Learning to Love Regular Expressions https://blogs.mathworks.com/loren/2012/10/18/learning-to-love-regular-expressions/?s_tid=feedtopost Loren Shure is interested in the design of the MATLAB language. She is an application engineer and writes here about MATLAB programming and related topics. Tue, 06 Nov 2012 16:37:42 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Sarah Wait Zaranek https://blogs.mathworks.com/loren/2012/10/18/learning-to-love-regular-expressions/#comment-33269 Tue, 06 Nov 2012 16:37:42 +0000 https://blogs.mathworks.com/loren/?p=557#comment-33269 @Antonio –

You are welcome. Glad you liked it.

]]>
By: Antonio https://blogs.mathworks.com/loren/2012/10/18/learning-to-love-regular-expressions/#comment-33253 Thu, 01 Nov 2012 15:23:15 +0000 https://blogs.mathworks.com/loren/?p=557#comment-33253 I like this article. I use to work with matlab to calculate numbers but It’s also a fantastic tool to work with texts. The normal applications need to display some texts to improve their understanding.
Thanks you for this article.

]]>
By: Sarah Wait Zaranek https://blogs.mathworks.com/loren/2012/10/18/learning-to-love-regular-expressions/#comment-33232 Fri, 26 Oct 2012 13:37:09 +0000 https://blogs.mathworks.com/loren/?p=557#comment-33232 @Jaromir –

Cool!

I knew there was a nice way to do it with replacement, but I was torn because I wanted to focus first on regexp. Thanks for sharing this – because ultimately it is probably the most efficent and cleanest way to do it.

]]>
By: Sarah Wait Zaranek https://blogs.mathworks.com/loren/2012/10/18/learning-to-love-regular-expressions/#comment-33230 Fri, 26 Oct 2012 13:31:56 +0000 https://blogs.mathworks.com/loren/?p=557#comment-33230 @Aurélien –

Maybe those flashcards would be good to share on File Exchange. A cheat sheet for people to hang in their offices might be really nice.

]]>
By: Jaromir Benes https://blogs.mathworks.com/loren/2012/10/18/learning-to-love-regular-expressions/#comment-33228 Thu, 25 Oct 2012 16:27:28 +0000 https://blogs.mathworks.com/loren/?p=557#comment-33228 Yet another (and more compact) solution to the problem in Example #2

regexprep(locationNames,'([A-Z].)[^A-Z]*’,’$1′)

:)

]]>
By: Sarah Zaranek https://blogs.mathworks.com/loren/2012/10/18/learning-to-love-regular-expressions/#comment-33223 Mon, 22 Oct 2012 20:04:11 +0000 https://blogs.mathworks.com/loren/?p=557#comment-33223 @Sven –

I chatted with development, and let them know of your request. However, right now I think your way of doing it as “easy” as it can get. There isn’t anything that you are missing that I am aware of. I will try and think about it some more and see if anything else pops into my brain.

Thanks,
Sarah

]]>
By: Sven https://blogs.mathworks.com/loren/2012/10/18/learning-to-love-regular-expressions/#comment-33220 Sat, 20 Oct 2012 14:43:36 +0000 https://blogs.mathworks.com/loren/?p=557#comment-33220 One question about using regexp in a strcmp-like way…

>> strCell = {‘first’,’second’,’third’,’fourth’};

Using strcmp gives me a nice little logical mask that matches my criteria:

>> strcmp(‘third’,strCell) % [0 0 1 0]

Let’s say I had some criteria that needed regular expressions, and wanted to get a logical mask telling me which cells matched my criteria.

>> regexp(strCell,'(f|r)’)

ans =

[1×2 double] [] [4] [1×2 double]

That, of course can be simplified a bit by using ‘once’:

>> regexp(strCell,'(f|r)’,’once’)

ans =

[1] [] [4] [1]

But it’s still not a logical mask. The only way I can think to make it one is to wrap it in a cellfun like this:

>> ~cellfun(@isempty, regexp(strCell,'(f|r)’))

ans =

1 0 1 1

Now, the above of course works just fine, but it just feels a touch awkward… Asking “which strings match my pattern” seems like a very simple (and very often asked!) question, but getting an answer to it involves the negation of a cellfun() call.

It feels like strcmp() is nice and straight forward, but adding the (relatively minor) addition of matching a pattern instead of an exact string takes a detour into logic and functions that makes the (notoriously confusing) regular expressions that much more tricky in MATLAB.

Thoughts? Have I simply missed an obvious regexp() parameter that would make it behave how I want it to?

]]>
By: Aurélien https://blogs.mathworks.com/loren/2012/10/18/learning-to-love-regular-expressions/#comment-33217 Fri, 19 Oct 2012 07:21:52 +0000 https://blogs.mathworks.com/loren/?p=557#comment-33217 Like you, it is when I started playing CODY that I have noticed the power of regular expressions … therefore I started writing some flashcards to aid memorization !
Thanks for this article

]]>