Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 87029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:20:20+00:00 2026-05-10T22:20:20+00:00

It seems that the choice to use string parsing vs. regular expressions comes up

  • 0

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, information about said string, etc.

The reason that this comes up is that we’re evaluating a soap header’s action, after it has been parsed into something manageable via the OperationContext object for WCF and then making decisions on that. Right now, the simple solution seems to be basic substring’ing to keep the implementation simple, but part of me wonders if RegEx would be better or more robust. The other part of me wonders if it’d be like using a shotgun to kill a fly in our particular scenario.

So I have to ask, what’s the typical threshold that people use when trying to decide to use RegEx over typical string parsing. Note that I’m not very strong in Regular Expressions, and because of this, I try to shy away unless it’s absolutely vital to avoid introducing more complication than I need.

If you couldn’t tell by my choice of abbreviations, this is in .NET land (C#), but I believe that doesn’t have much bearing on the question.


EDIT: It seems as per my typical Raybell charm, I’ve been too wordy or misleading in my question. I want to apologize. I was giving some background to help give clues as to what I was doing, not mislead people.

I’m basically looking for a guideline as to when to use substring, and variations thereof, over Regular Expressions and vice versa. And while some of the answers may have missed this (and again, my fault), I’ve genuinely appreciated them and up-voted as accordingly.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:20:20+00:00Added an answer on May 10, 2026 at 10:20 pm

    My main guideline is to use regular expressions for throwaway code, and for user-input validation. Or when I’m trying to find a specific pattern within a big glob of text. For most other purposes, I’ll write a grammar and implement a simple parser.

    One important guideline (that’s really hard to sidestep, though I see people try all the time) is to always use a parser in cases where the target language’s grammar is recursive.

    For example, consider a tiny ‘expression language’ for evaluating parenthetized arithmetic expressions. Examples of ‘programs’ in this language would look like this:

    1 + 2 5 * (10 - 6) ((1 + 1) / (2 + 2)) / 3 

    A grammar is easy to write, and looks something like this:

    DIGIT := ['0'-'9'] NUMBER := (DIGIT)+ OPERATOR := ('+' | '-' | '*' | '/' ) EXPRESSION := (NUMBER | GROUP) (OPERATOR EXPRESSION)? GROUP := '(' EXPRESSION ')' 

    With that grammar, you can build a recursive descent parser in a jiffy.

    An equivalent regular expression is REALLY hard to write, because regular expressions don’t usually have very good support for recursion.

    Another good example is JSON ingestion. I’ve seen people try to consume JSON with regular expressions, and it’s INSANE. JSON objects are recursive, so they’re just begging for regular grammars and recursive descent parsers.


    Hmmmmmmm… Looking at other people’s responses, I think I may have answered the wrong question.

    I interpreted it as ‘when should use use a simple regex, rather than a full-blown parser?’ whereas most people seem to have interpreted the question as ‘when should you roll your own clumsy ad-hoc character-by-character validation scheme, rather than using a regular expression?’

    Given that interpretation, my answer is: never.


    Okay…. one more edit.

    I’ll be a little more forgiving of the roll-your-own scheme. Just… don’t call it ‘parsing’ :o)

    I think a good rule of thumb is that you should only use string-matching primitives if you can implement ALL of your logic using a single predicate. Like this:

    if (str.equals('DooWahDiddy')) // No problemo.  if (str.contains('destroy the earth')) // Okay.  if (str.indexOf(';') < str.length / 2) // Not bad. 

    Once your conditions contain multiple predicates, then you’ve started inventing your own ad hoc string validation language, and you should probably just man up and study some regular expressions.

    if (str.startsWith('I') && str.endsWith('Widget') &&     (!str.contains('Monkey') || !str.contains('Pox')))  // Madness. 

    Regular expressions really aren’t that hard to learn. Compared to a huuuuge full-featured language like C# with dozens of keywords, primitive types, and operators, and a standard library with thousands of classes, regular expressions are absolutely dirt simple. Most regex implementations support about a dozen or so operations (give or take).

    Here’s a great reference:

    http://www.regular-expressions.info/

    PS: As a bonus, if you ever do want to learn about writing your own parsers (with lex/yacc, ANTLR, JavaCC, or other similar tools), learning regular expressions is a great preparation, because parser-generator tools use many of the same principles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Portability: Linux and Windows at least I hope. What about… May 12, 2026 at 1:12 am
  • Editorial Team
    Editorial Team added an answer If the plugins are only really meant to override one… May 12, 2026 at 1:12 am
  • Editorial Team
    Editorial Team added an answer Heap space is NOT always set to zero. May 12, 2026 at 1:12 am

Related Questions

I am looking for a way to get hibernate to use oracle's SYS_GUID() function
I have an object that I want to use to look up other objects.
Concerning headers in a library, I see two options, and I'm not sure if
I can't think of a good title, but my question is not as naive

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.