• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Why regular expressions

 
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one explain the reason for using reg expressions.
What is the true definition of a 'Regular expression'.
In what contexts this can be used.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider input validation, for example. You need to verify that a given String represents a valid email address -- how do you do that? You need to check the input against certain rules such as "the left side of '@' represents a user" and "the right side of '@' represents a valid domain name". You could do this with a set of "if (...) { return false; }" style guard clauses or you could use regular expressions which are less verbose (albeit require more skills to read).
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got the point.
 
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A more formal definition:
A regular expression (abbreviated as regexp or regex) is a string that describes a whole set of strings, according to certain syntax rules. These expressions are used by many text editors and utilities (especially in the Unix operating system) to search bodies of text for certain patterns and, for example, replace the found strings with a certain other string.
As indicated by the previous post they are very compact, flexible and powerful. If you need to do complex string manipulation they are critical. I'd recommend playing with them to simply get a feel for the power and it will help you see where they are applicable in your own projects.
 
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From memory REs can be defined mathematically as the set of strings accepted by a finite state automata or generated by a context-free grammar... but at this point the definition isn't very useful for anyone but the mathematically inclined.
I might have got that wrong - there are enough classes of automata and grammars for my memory to be a tad shakey...given the density of well-informed people in these forums no doubt somebody can correct me
-Tim
[ April 13, 2004: Message edited by: Tim West ]
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All of the above is correct, if a bit formal. My suggestion is to think of regex as a sort of SQL you can apply to files.
All best,
M
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice feedback.
One last question: "Are regular expressions used extensively in the industry/ or is it just a fad for complex solutions driven delopers".
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Kishore Dandu:
Are regular expressions used extensively in the industry/ or is it just a fad for complex solutions driven delopers


Yes, regular expressions are used extensively, although mostly in Perl-driven applications/programs/utilities.
 
Max Habibi
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's solution for lazy developers, to be honest. They sound complex, but they're really all about making your life easier. The challenge is to not be intimidated, and see what's there for you.
for example, say you need to validate that a String contains a number. You might use

or that it's a valid e-mail address. You might use

Or you want to invert the order of the first name and the last name: that is, turn "John Smith" into "Smith, John"

or validate a zip code

These are just the easy, trivial examples: We haven't gotten the actual regex objects, like Pattern and Matcher.
HTH,
M
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for enlightening me some more.
 
Liar, liar, pants on fire! refreshing plug:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!