Honesty Rocks! truth rules.

Regular Expressions

HOME      >>       Programming

FirefoxRocks

In PHP and many other programming languages, I have heard of this powerful syntax called RegEx, or regular expressions. It allows you to find strings that match a certain pattern.

I had to use a regex expression to search for a specific string of text within a block of text once, and someone told me to use a regular expression. The example that they wrote worked, although it was extremely confusing and I didn't know what it meant.

http://www.regular-expressions.info/ gives a great tutorial on regular expressions, the syntax and stuff. I'm not sure if this is worth learning. Is it?

How often do I have to use regular expressions? They seem to be really powerful? Is it worth the time to figure them out?


toby

You'll probably be learning PCRE regex, orignally from Perl. Regex is very powerful, but one of the slowest general use functions in php. You really should learn if if you can, it's very language-independant. If you look at http://php.net/preg_match (php called it preg, others called it regex) for an explanation of how the regex patterns are used. I found the website you linked to quite hard at first, but the language I used had a much simpler explanation elsewhere. Php may do, you could google for one.


wutske

How often you use it depends on what you usualy do :mellow: . I personally think that knowing how to use regular expressions is a good thing, they can realy save a lot of time when you are looking for something (eg. when searching a mysql database). Some programs (like Notepad++) allow you to use regular expressions when searching something in a file, very handy :D


FirefoxRocks

You'll probably be learning PCRE regex, orignally from Perl. Regex is very powerful, but one of the slowest general use functions in php. You really should learn if if you can, it's very language-independant. If you look at http://php.net/preg_match (php called it preg, others called it regex) for an explanation of how the regex patterns are used. I found the website you linked to quite hard at first, but the language I used had a much simpler explanation elsewhere. Php may do, you could google for one.

Yes, I believe that the version I'm learning is PCRE regex, or at least that is what I'm testing it on anyways. The PHP page only explained how to use the function a few basic examples like word boundaries and negated searching.

 

How often you use it depends on what you usualy do :mellow: . I personally think that knowing how to use regular expressions is a good thing, they can realy save a lot of time when you are looking for something (eg. when searching a mysql database). Some programs (like Notepad++) allow you to use regular expressions when searching something in a file, very handy :D

I realize that Notepad++ has an option for finding text using a regular expression, but it doesn't work. The site linked above gave a few (commercial) programs that use regular expressions to "save time and work". I guess that knowing how to use regular expressions is very useful in validating forms such as email addresses and stuff, and also as wutske stated, for searching databases.

wutske

Yes, I believe that the version I'm learning is PCRE regex, or at least that is what I'm testing it on anyways. The PHP page only explained how to use the function a few basic examples like word boundaries and negated searching. I realize that Notepad++ has an option for finding text using a regular expression, but it doesn't work. The site linked above gave a few (commercial) programs that use regular expressions to "save time and work". I guess that knowing how to use regular expressions is very useful in validating forms such as email addresses and stuff, and also as wutske stated, for searching databases.


Strange, I've used regular expressions in notepad++ before and it does work.

Quickly write a simple html page in Notepad++ :
<html> <head> <title>Hello dude</title> </head> <body> <h1>This is a very rudimentary webpage</h1> <p>Some custom content</p> </body></html>

And use the following regular expression to find all html tags (without attributes that is).
<[A-Za-z][A-Za-z0-9]*>


FirefoxRocks

It didn't work on an HTML page, but it worked in an XML document.So I guess with arbitrary text support, regular expressions really are useful.


docduke

And use the following regular expression to find all html tags (without attributes that is).

<[A-Za-z][A-Za-z0-9]*>
I guess what you mean depends on what "all html tags" means. I don't think your regex will find any closing tags. I'm barely fluent in regex, but I suspect you really wanted <[A-Za-z\/][A-Za-z0-9]*>, where I have "quoted" the forward slash, in case it is treated as a special character within the regex. What makes things like this really confusing is that some "ordinary" characters, like period, have special meanings. :mellow:

x0rk

Perl regex is the most powerful and best regex to learn... I write website scrapers using perl regex... :mellow:


PureHeart

In short, regex is a must for those dealing with text processing. Some of its popular applications include form validation (client side with JavaScript and server side with PHP), text parsing, etc. One more thing, regex is actually very simple. It was covered in a 30 minute lecture at my university. Is there anyone here interested in learning regex? (I can post a tutorial then)


iGuest

Regular expression for a text phrase/patternRegular Expressions

Hello,

I'm on this moment busy and I'm not familiar with regular expressions. I'm working with notepad++and I want to replace certain text string/patterns. It start always the same with href=" and it ends with ">. And I want these replace this with nothing.

Can somebody help me with a simple example.

Thanks in advance

Nico

-question by Nico van de Kamp