LinuxQuestions.org
Help answer threads with 0 replies.
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 10-19-2011, 09:14 AM   #1
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,552

Rep: Reputation: 103Reputation: 103
Emacs regex headache - replace between ' and ' ??


Hi guys

I have strings that looks like this in Emacs:

$_POST['banner_name']
$_POST['banner_alt_text']
$_POST['banner_location_type']

I'm searching for an Emacs regex that will match the whole string, e. g

$_POST['

and then any number of characters up to the next

']

My problem is how do I "say" to emacs "match up to the next ' "?

I so far have

\$_POST['

which works but how do I now "say" "from the ' to the second (irrespective of character count) "'" character and then the ] at the end?

Thanks!
 
Old 10-19-2011, 10:02 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well normal way is to say give me all that is not (I do not know emacs so you may have to play with it):
Code:
\$_POST['[^']*']
I would have thought you might need to escape the first and last square bracket??
 
Old 10-19-2011, 03:18 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by grail View Post
Well normal way is to say give me all that is not (I do not know emacs so you may have to play with it):
I don't know if Emacs supports this, but some regex flavors let you put a "?" after a "*" or "+" to make it lazy instead of greedy (that is, it will match as little text as possible). Here is an example:

Code:
\$_POST\['.*?'\]
Quote:
Originally Posted by grail View Post
I would have thought you might need to escape the first and last square bracket??
That's probably a good idea.
 
Old 10-20-2011, 02:48 AM   #4
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,552

Original Poster
Rep: Reputation: 103Reputation: 103
Hi guys

Thanks for the suggestions!

I tried the following:

Code:
\$_POST\['[^]*?']
\$_POST\['[^]*+']
\$_POST\['[^]*-']
However, all variations only match

$_POST[b

Also

Code:
\$_POST\['.*?'\]
is Emacs invalid apparently...

E. g. I've still got the same problem, I can match the fixed parts of the string no problem, but the varying part between the ' ' quotes - how to specify "match any number of characters up to next ' " still eludes me.

Thanks for the assistance anyway!

Last edited by rylan76; 10-20-2011 at 02:50 AM.
 
Old 10-20-2011, 03:32 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Did you try any of your first examples without the extra character after the asterisk?
Code:
\$_POST\['[^]*?']

# so just have
\$_POST\['[^]*']
 
Old 10-20-2011, 03:38 AM   #6
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,552

Original Poster
Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by grail View Post
Did you try any of your first examples without the extra character after the asterisk?
Code:
\$_POST\['[^]*?']

# so just have
\$_POST\['[^]*']
Hi grail

Yip, I tried

Code:
\$_POST\['[^]*']
and also

Code:
\$_POST\['[^]*'\]
and

Code:
\$_POST\['[^]*'\]]
but like the rest, the first two only matches

$_POST['b

and the last is completely invalid - e. g. the two working ones just the first char between the quotes - instead of all the chars between the quotes...

E.g. it is the right track, I just need to get it to be "greedy" up to the next ' it encounters.

Thanks for the help.

Kind regards,

Last edited by rylan76; 10-20-2011 at 03:41 AM.
 
Old 10-20-2011, 06:29 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I just looked back over all examples and realised you are not asking it to look for not an apostrophe??
Code:
\$_POST\['[^]*']

# should be

\$_POST\['[^']*']
If this doesn't work try adding the apostrophe back to your other tests.
 
1 members found this post helpful.
Old 10-20-2011, 08:15 AM   #8
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,552

Original Poster
Rep: Reputation: 103Reputation: 103
Quote:
Originally Posted by grail View Post
I just looked back over all examples and realised you are not asking it to look for not an apostrophe??
Code:
\$_POST\['[^]*']

# should be

\$_POST\['[^']*']
If this doesn't work try adding the apostrophe back to your other tests.
Mr. Grail you are a GENIUS

Your bottommost suggestion worked perfectly.

Thank you VERY much for your assistance - I've marked your post as helpful.

I'll go back to the Emacs docs now and see what the syntax exactly means - still learning this side.

Again, thank you.

Keep well

Stefan
 
Old 10-20-2011, 09:08 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Cool ... glad we got there .. don't forget to mark as SOLVED.
 
  


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
[SOLVED] Bash - String replace at the end with a regex myro Linux - General 7 09-27-2011 04:37 PM
bash replace all matches of regex substring in string nickleus Linux - General 3 04-30-2011 11:08 AM
[SOLVED] Simple regex match without replace? nrg Programming 1 11-21-2009 03:04 AM
Sed and regex: how to replace up to a certain string olliesa Linux - General 2 10-21-2009 02:49 AM
PHP regex replace? iluvatar Programming 5 09-10-2004 10:03 AM

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

All times are GMT -5. The time now is 07:05 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