LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-05-2004, 10:59 AM   #1
E-Oreo
LQ Newbie
 
Registered: Dec 2002
Posts: 24

Rep: Reputation: 15
Regular Expressions problem


I searched this forum and google before hand but couldn't find anything that helped me out. What I need to do is match a right bracket mark ( ] ) inside of brackets ( [] ). Actually I need to make that negative with ^ also.

I am trying to match
Code:
[say=Hello Word]
Where the text Hello World can be anything but the [say=] will always be the same. Using instinct I tried:
Code:
\[say=[^\]]*]
but can't get it to work, I know I'm doing something wrong but does anything know how to match a ] inside of []?
 
Old 04-05-2004, 11:51 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
I might misunderstand the question, but I don't think you need the double [[]] construct
If you want to match this: [say=Hello Word] and Hello World can be anything, this will match what you want:

\[say=.*\]

The .* matches anything (including nothing) but it has to start with \[say= and end with \]. You need to escape them, otherwise everything in between is seen as possibilities for the regular expression, you need them literaly.

Like I said, I could have misunderstood the question. Hope this helps anyway
 
Old 04-05-2004, 12:58 PM   #3
E-Oreo
LQ Newbie
 
Registered: Dec 2002
Posts: 24

Original Poster
Rep: Reputation: 15
That would work except that if they use it twice or use another ] mark later it might match to more text that I want it to.

Example:
Code:
Message == "test [say=say test] testing more :]
In the above example that regexp would match "[say=say test[testing more:]", is there a way to switch the PHP ereg function and the javascript string.replace function to ungreedy?
 
Old 04-05-2004, 02:16 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
I guess I don't understand what it is you want.

"test [say=say test] testing more :]"
"[say=say test[testing more:]"

Both get valid hits by what you described earlier (Hello World can be anything, but the [say=] will always be the same). Both have the [say= and the ending ] and some text in between.

Using sed, this is what I get:

$ echo $TESTING
test [say=say test] testing more :]

Show all, except what's in between [say= and the ending ]
$ echo $TESTING | sed 's/\(\[say=\).*\(\]$\)/\1\2/'
test [say=]

Show all, except [say= and the ending ]
$ echo $TESTING | sed 's/\(\[say=\)\(.*\)\(\]$\)/\2/'
test say test] testing more :

And for the second example:

echo $TESTING
[say=say test[testing more:]

Show all, except what's in between [say= and the ending ]
$ echo $TESTING | sed 's/\(\[say=\).*\(\]$\)/\1\2/'
[say=]

Show all, except [say= and the ending ]
$ echo $TESTING | sed 's/\(\[say=\)\(.*\)\(\]$\)/\2/'
say test[testing more:

Both look correct to me, but we probably are talking about different things

I'm not familiar with javascript and have some experience with PHP, cannot tell you if there is a function in PHP that would do what you want.

Could/would you elaborate on what it is you are trying to do.
 
Old 04-05-2004, 03:01 PM   #5
E-Oreo
LQ Newbie
 
Registered: Dec 2002
Posts: 24

Original Poster
Rep: Reputation: 15
I am working on a chat room, when the message is sent I need to format certain tags that contain style information. One of these is the color tag that has the syntax: Black text or you can do black text. The problem is that the PHP eregi function (which finds a match based on a regular expression you give it) is greedy, so it doesn't stop when it has a match (ie. string = "[color=red]red text[/red] black text", regexp = \[color=.*\], it returns "[color=red]red text[/red]" since it starts with [color= and ends in ] but it's the biggest possible thing that it can match and I need it to return when it hits the first ] not the last ] so it only returns "[color=red]".

Anyone know how to do this? (It's most important in javascript, I can use preg_match in PHP to make it ungreedy)
 
Old 04-05-2004, 03:20 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
I'm clear about your problem and it's a nice one.......

Have to think about it for a bit, but I don't think it can be done with just a regular expression (not knowing what javascript can/cannot do). This will take a few lines of code.

First that comes to mind is counting the amount of [say= that are in a line. If there's more then one, chop the line to chunks and work your magic on one chunck at the time. Will let you know if I find a(n elegant) solution.
 
Old 04-05-2004, 04:04 PM   #7
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
How about something like: "\[say=.*[^\]].*\]"

This is just off the top of my head, but that should match
[say=<anything other than a ]>]
 
Old 04-05-2004, 05:02 PM   #8
E-Oreo
LQ Newbie
 
Registered: Dec 2002
Posts: 24

Original Poster
Rep: Reputation: 15
Ok, I tried again with :

\[size=[^\]]*\] and got it to work, the code above didn't because of hte .* after the [^\]] that matched everything to the end of hte string if there was a second ] mark.

I also got it to work with \[size=(.+?)?\] but I have no clue whta (.+?)? means, if anyone has a minute could they explain that part please.

Thanks for the help everyone RegExps are difficult for me
 
Old 04-06-2004, 04:18 AM   #9
smaida
Member
 
Registered: Apr 2004
Location: Richmond, VA - USA
Distribution: Debian
Posts: 62

Rep: Reputation: 15
This might help...

I can't say that I know anything at all about php... but I do know that perl has the same issue. Perl's matching by default is greedy and always try to find the largest match possile. You can use a ? to tell perl to be stingy in the match.

For example..

If you used the regex /a.+b/ to match "a xxx b funregex b" you find that perl matches the whole string.

If we wanted perl to just match the "a xxx b" we would use

/a.+?b/

This tells perl to stop being greedy directly after the .+ and to stop matching at the first "b".

I hope this helps.

-Shawn
 
Old 04-09-2004, 11:14 AM   #10
E-Oreo
LQ Newbie
 
Registered: Dec 2002
Posts: 24

Original Poster
Rep: Reputation: 15
Ah, thank you. That makes sense and that seems to be the way it is acting also. I believe regular expressions are pretty much the same in all programming languages and I have been able to use the same regular expressions for perl, php and javascript fine.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions markjuggles Programming 2 05-05-2005 11:39 AM
Regular Expressions overbored Linux - Software 3 06-24-2004 02:34 PM
help with REGULAR EXPRESSIONS ner Linux - General 23 10-31-2003 11:09 PM
Regular expressions aromes Linux - General 1 10-15-2003 12:29 PM
regular expressions? alaios Linux - General 2 06-11-2003 03:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration