When is it best to use Regular Expressions over basic string splitting / substring'ing?

前端 未结 7 1616
旧巷少年郎
旧巷少年郎 2020-12-01 12:46

It seems that the choice to use string parsing vs. regular expressions comes up on a regular basis for me anytime a situation arises that I need part of a string, informatio

7条回答
  •  抹茶落季
    2020-12-01 13:06

    [W]e're evaluating a soap header's action and making decisions on that

    Never use regular expressions or basic string parsing to process XML. Every language in common usage right now has perfectly good XML support. XML is a deceptively complex standard and it's unlikely your code will be correct in the sense that it will properly parse all well-formed XML input, and even it if does, you're wasting your time because (as just mentioned) every language in common usage has XML support. It is unprofessional to use regular expressions to parse XML.

    To answer your question, in general the usage of regular expressions should be minimized as they're not very readable. Oftentimes you can combine string parsing and regular expressions (perhaps in a loop) to create a much simpler solution than regular expressions alone.

提交回复
热议问题