10

I want to ask a question about methodology in Swift, of how to parse a string of a given format into a list of names and dates, but when I go to type it, Stack Overflow always alerts me that other similar questions have been downvoted before. I have seen other questions that are related and have tested those approaches, but my situation hasn't been asked about before. I've done some research on various methods, but I'm at a stop of how to go further, which is why I want to ask a question. How can I ask a question about how to do something in such a way that will lead to it not being downvoted? My question text would likely be something like this:

I'm working on a project where I am given a user and a birthdate for them, along with a list of movies and dates that the person has gone to. An example string is something like this: "Participant Name: Example name, Birthdate: 01/11/2000, Spiderman 05/15/2021 07/16/2021 08/17/2021 Avengers Infinity War 05/15/2020 07/16/2020 08/17/2020 The Lorax 01/05/2015" and so on. I know which movies the string will contain, and I know the maximum amount of dates per movie, but I don't know the specific number of times the person would have seen the movie. I've done the following for the birthdate, which works only because the request is always formatted the same way in terms of birthdates:

func FindBirthdate(str: String) -> Date{
  //I make sure that the text before birthdate is always converted to DOB 
//in other functions, and that the string is converted 
//to an array seperated by spaces.
        let index = str.firstIndex(of: "DOB:")!
   let birthdate = str[index+1]
   print(birthdate)
   let formatter = DateFormatter()
   formatter.dateFormat = "MM/dd/yyyy"
   formatter.locale = Locale(identifier: "COUNTRY IDENTIFIER")
   formatter.timeZone = TimeZone(identifier: "EXAMPLE")
   return formatter.date(from: birthdate) ?? Date()
}
    
}

However, as I previously stated, I don't know how many times the user would have seen the movie. The movies will all be in the same order. How would I split the characters in between each movie into dates for that movie? Once again, my problem is that I don't know the number of dates, so how would I get each date and return a list? I've looked into a sort of ForEach statement, but I'm not sure how I could integrate that into a string. This answer suggested that I use regexes, however, this solely focuses on the dates, and not the movies. The string isn't solely made up of dates. I've also taken a look at sample date parsing in Swift, but that is about just single dates. My issue isn't date conversion, it's finding and separating the dates in the first place.

The reason that this question hasn't been asked before is that though other questions may ask about date parsing or finding substrings, I need to find each individual date for each movie and the movie title - this is trying to find each individual date in the string for each movie.

Side note: A comment asked why those other questions were downvoted. To quote another comment,

Many parsing questions are thinly veiled "do my work" requests – bock standard tasks with poorly worded requirements and little reusability.

11
  • 9
    I don't frequent the swift tag, but in the ones I do frequent many parsing questions are thinly veiled "do my work" requests – bock standard tasks with poorly worded requirements and little reusability. They get downvoted for the "do my work" part, not the "parsing" part. If you focus on the actual challenge of the task, point out why it's non-trivial/unique, and have a clear problem statement, then the question should be much better received than most others. May 31, 2022 at 16:56
  • If you provide the text of your potential question here before posting, we can likely provide input on what might cause friction before you even have to ask it. May 31, 2022 at 17:03
  • 2
    Re "my situation hasn't been asked about before.": What is special about it? What kind of parsing? (Not rhetorical questions.) There are 313,706 Swift questions. May 31, 2022 at 17:13
  • Isn't split by a delimiter (or even a regular expression) a thing in Swift? (I am guessing it is about split.) May 31, 2022 at 17:30
  • Yes, it is, but I'm not sure exactly what I would split the string by.
    – user18457910
    May 31, 2022 at 17:32
  • 1
    Somewhat related: The (in)famous Zaldo answer (about inappropriate use of regular expressions for parsing. "Even Jon Skeet cannot parse HTML using regular expressions."). Isn't there a corresponding meta question? May 31, 2022 at 18:00
  • I could imagine a Stack Overflow question about parsing some custom format (not XML, JSON, CSV, HTML, YAML, some standard medical exchange format monstrosity, etc.) exists where the answer would be a general description of how to make a BNF-based parser (for example, in Swift). Does it exist? May 31, 2022 at 18:10
  • Not that I've yet found - I've searched SO but I haven't found anything about making a custom BNF parser.
    – user18457910
    May 31, 2022 at 18:20
  • 1
    BNF [swift] only returns 3 hits of which 2 are false positives. That leaves one (but it is only a lead). It is time for the search engine sharks to move in. May 31, 2022 at 18:24
  • 1
    "How can I ask a question about how to do something in such a way that will lead to it not being downvoted?" That depends on why those questions were downvoted. Jun 1, 2022 at 2:35
  • 1
    Just ask different. (Sorry). Jun 2, 2022 at 15:32

2 Answers 2

11

Extra guidances for "how to parse my string" questions:

  • show valid/invalid examples, and not just one. The choice of examples would immediately show how much understanding and research you have done.
  • clarify where values come from and why there is no more structured way to parse those. Be careful not to show examples of parsing JSON/HTML/XML with regexes or any string manipulation - those well-known text formats have solid parsers for ages and would cause flood of trolling comments and downvotes.

One very common example are names - what are names for your particular case:

  • just Latin letters and spaces - "John Doe"
  • some non-letters - "Joe O'Connor"
  • commas - "Joe, the second"
  • any other Unicode letter or digit characters
  • flat out trolling parents picking names for their kids (hello from Elon Musk)

Showing list of things that can/can't be present in the value helps to find out what can be used as delimiters.

In your case "movie name" is even more entertaining - can it have numbers? What about "Attack of crows on 5/5/2025" premiering on "5/5/2025"?

And finally dates - that is the best thing next to sliced bread. Is date format fixed for all strings to parse or can vary? Can string come formatted from different countries that use different mm/dd or dd/mm order? What do you do with strings that just look like dates but in reality are either not dates or ambiguous - "1/2/3"? If you can't enforce ISO 8601 for dates in your input you can as well just pick random dates and it will be good enough.

3
  • Your Answer (=> Guidance) is geared at already providing a Solution using REGEX, you could also mention Formatting (for the (Sub-)Strings) and the Separator's between those Strings and Substrings if one wants to provide a Solution using split() (or the Equivalent in Swift)...
    – chivracq
    May 31, 2022 at 22:01
  • I've edited my answer to include what you asked - the movies will always be a certain set, none of which have numbers or special characters. The dates will always be in the same format. @chivracq, what do you mean by mentioning formatting?
    – user18457910
    Jun 1, 2022 at 0:06
  • Well, the "Format" used to input any single String, what remains constant and what not, + again about the Separators between each String and all Substrings (= Name (x1), Birthdate (x1), [Film + Date (xn)] (xm)), within a String (= 1 Entry, 1 "Name").
    – chivracq
    Jun 1, 2022 at 1:20
9

Be absolutely certain that your question hasn't actually been asked before.

That is to suggest, search through the regrettably vast sea of questions before you and establish if someone has asked something similar to what you're attempting to do. If you've tried those other explanations, be explicit about why they don't work for your scenario.

Instead of something like:

I tried Solution X but it didn't work.

you would need to elaborate:

I tried Solution X, but it only applies if I leverage JSON data. I'm attempting to do this with YAML instead.

Put in just a bit more effort into the question you're going to ask and you won't really run the risk of it being downvoted. Or, you wouldn't run the risk of it being downvoted for very long.

0

You must log in to answer this question.