locked
How can I ensure there is a space before any numeric suffix in an image title? RRS feed

  • Question

  • User695372294 posted

    I help to run an annual international photo competition. The image titles submitted by our entrants frequently contain mistakes in spelling, grammar or punctuation. If these images do well enough in our competition then they are listed in our catalogue. At that stage, any errors would look like our errors and thus make us look unprofessional. I therefore do my best to eliminate all mistakes. Luckily, I am a fast proof reader, but I would like to automate the correction process as much as possible.

    An entrant with a sequence of images of the same topic often chooses to add a distinguishing numeric suffix. That is fine, but a common minor irritation is when such a suffix is actually added to the last word of the title without an intervening space, e.g. Bird on a Stick2, rather than Bird on a Stick 2.

    Can anyone suggest a VB.NET string function that would automatically insert the missing space in such circumstances?

    David   

    Sunday, December 29, 2013 7:07 AM

Answers

  • User281315223 posted

    Alright, you may be able to give this a try : 

    Function CleanUpTitle(ByVal s As String) As String
    	'Surround all digits with spaces'
    	Dim spaced = Regex.Replace(s,"(?<=\s)\d+|-|\d+$", Function(m) String.Format(" {0} ",m))
    		
    	'Consoliate all multiple spaces into single ones
    	spaced = Regex.Replace(spaced,"\s+"," ")
    	Return spaced.Trim
    End Function

    It will match the following and add spaces around them : 

    • Any digits that are preceeded with white space.
    • Any hyphens
    • Any digits that occur at the end of a string
    Console.WriteLine(CleanUpTitle("Photo 12"))         'yields "Photo 12"'
    Console.WriteLine(CleanUpTitle("Photo 123"))        'yields "Photo 123"'
    Console.WriteLine(CleanUpTitle("Photo-99"))         'yields "Photo - 99"'
    Console.WriteLine(CleanUpTitle("Bird on a Stick2")) 'yields "Bird on a Stick 2"'
    Console.WriteLine(CleanUpTitle("Photo12 3"))        'yields "Photo 12 3"'
    Console.WriteLine(CleanUpTitle("Crash on M25 - 1")) 'yields "Crash on M25 - 1"'

    If you have any other issues situations that you find this doens't work for, let me know.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 30, 2013 11:44 AM

All replies

  • User-97501952 posted

    Hi,

    According to your description, I think you can make the numeric to string, then let the title string concat with it. For example:

    String.Concat(titlestring," ",numeric.ToString("N")).

    Hope this is helpful for you.

    Best Wishes,

    May 

    Monday, December 30, 2013 5:01 AM
  • User281315223 posted

    I'll provide examples for both scenarios (adding a space and removing it).

    Removing White Space prior to Digits ("Bird on a Stick 2" > "Bird on a Stick2")

    You could use a simple Regular Expression to match this and perform a replacement as seen below :

    Imports System.Text.RegularExpressions
    
    'Your original string'
    Dim original = "Bird on a Stick 2"
    'Removes any whitespace prior to a digit'
    Dim updated = Regex.Replace(original,"\s(?=\d)","")

    Adding White Space prior to Digits ("Bird on a Stick2" > "Bird on a Stick 2")

    The same basic principle applies, you'll simply match any trailing digits and add a space prior to any digits ; 

    Imports System.Text.RegularExpressions
    'Your original string' 
    Dim original = "Bird on a Stick2" 
    'Adds any whitespace prior to a digit that does not begin with white-space' 
    Dim updated = Regex.Replace(original,"(?<!\s)\d+",Function(m) String.Format(" {0}",m))

    Monday, December 30, 2013 8:30 AM
  • User695372294 posted

    Adding White Space prior to Digits ("Bird on a Stick2" > "Bird on a Stick 2")

    The same basic principle applies, you'll simply match any trailing digits and add a space prior to any digits ; 

    Imports System.Text.RegularExpressions
    'Your original string' 
    Dim original = "Bird on a Stick2" 
    'Adds any whitespace prior to a digit that does not begin with white-space' 
    Dim updated = Regex.Replace(original,"(?<!\s)\d+",Function(m) String.Format(" {0}",m))

    Hi Rion,
    Many thanks for that suggestion, but it has a few issues handling specific cases I should perhaps have made clear in my OP.

    For example:

    'Photo 12' is converted into 'Photo 1 2' rather than leaving it as 'Photo 12' 

    'Photo 123' is converted into 'Photo 1 23' rather than leaving it as 'Photo 123'

    'Photo-99' is converted into 'Photo- 99' rather than 'Photo -99' (or, even better, 'Photo - 99') 

    David

    Monday, December 30, 2013 10:21 AM
  • User281315223 posted

    If that is the case, you might want to simple wrap all of your sets of digits or hyphens with spaces and then consolidate any duplicate spaces into just a single one : 

    Function CleanUpTitle(ByVal s As String) As String
    	'Surround all digits with spaces'
    	Dim spaced = Regex.Replace(s,"\d+|-", Function(m) String.Format(" {0} ",m))
    		
    	'Consoliate all multiple spaces into single ones'
    	spaced = Regex.Replace(spaced,"\s+"," ")
    
            'Returns your result (removing any leading or trailing white-space'
        	Return spaced.Trim
    End Function

    Example Input / Output

    Photo12          > Photo 12
    Photo 12         > Photo 12 
    Photo 123        > Photo 123
    Photo-99         > Photo - 99
    Bird on a Stick2 > Bird on a Stick 2

    Monday, December 30, 2013 10:44 AM
  • User695372294 posted

    I was just about to reply saying that all was now well when I noticed that 'Photo12 3' is converted to 'Photo 12 3' rather than left as 'Photo12 3'. I don't really want to mess with numbers that are part of the title text rather than a suffix.

    Sorry to be a pain ... Smile

    David

    Monday, December 30, 2013 11:02 AM
  • User695372294 posted

    A plausible real world image title could be 'Crash on M25 - 1", which the current code would convert to 'Crash on M 25 - 1' (the M25 is probably the best know motorway name in the UK).

    David

    Monday, December 30, 2013 11:17 AM
  • User281315223 posted

    Alright, you may be able to give this a try : 

    Function CleanUpTitle(ByVal s As String) As String
    	'Surround all digits with spaces'
    	Dim spaced = Regex.Replace(s,"(?<=\s)\d+|-|\d+$", Function(m) String.Format(" {0} ",m))
    		
    	'Consoliate all multiple spaces into single ones
    	spaced = Regex.Replace(spaced,"\s+"," ")
    	Return spaced.Trim
    End Function

    It will match the following and add spaces around them : 

    • Any digits that are preceeded with white space.
    • Any hyphens
    • Any digits that occur at the end of a string
    Console.WriteLine(CleanUpTitle("Photo 12"))         'yields "Photo 12"'
    Console.WriteLine(CleanUpTitle("Photo 123"))        'yields "Photo 123"'
    Console.WriteLine(CleanUpTitle("Photo-99"))         'yields "Photo - 99"'
    Console.WriteLine(CleanUpTitle("Bird on a Stick2")) 'yields "Bird on a Stick 2"'
    Console.WriteLine(CleanUpTitle("Photo12 3"))        'yields "Photo 12 3"'
    Console.WriteLine(CleanUpTitle("Crash on M25 - 1")) 'yields "Crash on M25 - 1"'

    If you have any other issues situations that you find this doens't work for, let me know.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 30, 2013 11:44 AM
  • User695372294 posted

    Trailing spaces can prevent your code from working properly but I fixed that by adding Trim(), as shown below.

    Regex.Replace(Trim(s),"(?<=\s)\d+|-|\d+$", Function(m) String.Format(" {0} ",m))

    Many thanks for all your assistance. It's much appreciated. I plan to buy a regular expressions textbook so that I can work these things out for myself. My usual approach is to look for 5 star reviews on Amazon but are there any books you particularly recommend?

    David 

    Monday, December 30, 2013 12:48 PM
  • User281315223 posted

    Regular Expressions are one of those tricky categories in my opinion and learning how to use them effectively basically stemmed from working with them over a long period of time.

    I usually spent a good bit of time using RegexPal, which is an online tool that allows you to play with and test things out in real-time. Another great resource I would recommend would be to read through the tutorials and content over on RegularExpressions.info. It's a great site that covers a majority of the major things that you can do with Regular Expressions.

    Monday, December 30, 2013 2:33 PM