Re: Finding formatting items in a string



Hello Jack,

Hi there,

Given a standard .NET string, does anyone know what the regular
expression would be to locate each (optional) formatting item in the
string (or more likely does anyone have a link that will show me
this). For instance, given the following simple string:

"My phone number is {0} and my SSN is {1}"

I want to enumerate (or create a collection of) all formatting items
in the string which would be "{0}" and "{1}" in this (trivial)
example. The regular expression itself should handle all legal cases
of course (as described under "composite formatting" in MSDN - see
here: http://msdn2.microsoft.com/en-us/library/txafckwd.aspx). Any
help would be appreciated. Thanks.


The following expression will take care of most you want:

(?<!([^\{]|^)\{(\{{2})*)\{[0-9]+(,[-]?[0-9]+)?(:[^\}]+)?\}(?!\}(\{{2})*([^\}]|$))

I'll try to explain what it does:

(?<!([^\{]|^)\{(\{{2})*)
This part sees if we're dealing with an even number of opening {. In that case all are escaped and should therefore be ignored.
Due to the fact that there is no easy way to check for off or even numbers I've done it as follows: - first make sure we're either at the beginning of a line or that we match a character that is no {. That way we're sure where we're startign to count.
- Now chop off the first {, followed by any group of 2 extra {'s.

\{
- If that still leaves us with one {, then we're in business.

[0-9]+
- Now accept the numbered part. I've made it pretty simple here, any number will so.

(,[-]?[0-9]+)?
- Now accept the optional alignment. I think you could write the [-] as [+-], but I'm not sure from the top of my head that a plus is allowed for the alignment. I guess it is though.

(:[^\}]+)?
- Accept almost anything as optional formatting mask. As you can specify the formatting mask for each and every tipe differently based on the TypeFormatter, I guess there's no use in limiting the possible formats any way.
- So chop off everything that's not a closing }

\}
- Pick off the closing }

(?!\}(\{{2})*([^\}]|$))
- But only if it's followed by no or an odd number of closing }'s. This used the same logic as above.

You could make the regex more specific, but I guess this should get you started.

Also note that I haven't taken any whitespace into account, as I haven't had time to experiment where you would be allowed to add whitespace and where not.

If you still have any questions on how to improve or further limit the expression, feel free to ask.

--
Jesse Houwing
jesse.houwing at sogeti.nl


.



Relevant Pages

  • Re: New Python 3.0 string formatting - really necessary?
    ... When working on resource-oriented, generalized display applications ... ways - and on the web, display means "convert to a string for the ... the template knows how I ... I can solve this using the new string formatting. ...
    (comp.lang.python)
  • Re: Pulling from Access Query, Cannot Change Date/Time Formats
    ... With them you can specify DataFormatString to apply string ... With string formatting you get complete control over how date or ... Sub doColor ... Dim i9, i10 As Integer ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Track Changes VBA Granular information needed
    ... If the second string occurs somewhere in the first ... Bold, Italic". ... If you're keeping separate track of bold formatting and italic ... extract more specific changes. ...
    (microsoft.public.word.vba.beginners)
  • Re: Calculating Time Sheet
    ... show the time duration, but only if less than 24 hours. ... summing them and formatting the result as time will ... Public Function TimeElapsedAs String ... In query for instance you could have a computed column which calls the ...
    (microsoft.public.access.gettingstarted)
  • Re: wprintf() could not display unicode chars which is > 255 to console?
    ... > console window with formatting strings. ... Unicode and the Windows console is a tricky business. ... string msg, action; ... In Japanese you should have no spaces, and the translator can remove them. ...
    (microsoft.public.vc.mfc)

Loading