LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Emacs regex headache - replace between ' and ' ?? (https://www.linuxquestions.org/questions/programming-9/emacs-regex-headache-replace-between-and-908968/)

rylan76 10-19-2011 09:14 AM

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!

grail 10-19-2011 10:02 AM

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??

MTK358 10-19-2011 03:18 PM

Quote:

Originally Posted by grail (Post 4502576)
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 (Post 4502576)
I would have thought you might need to escape the first and last square bracket??

That's probably a good idea.

rylan76 10-20-2011 02:48 AM

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!

grail 10-20-2011 03:32 AM

Did you try any of your first examples without the extra character after the asterisk?
Code:

\$_POST\['[^]*?']

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


rylan76 10-20-2011 03:38 AM

Quote:

Originally Posted by grail (Post 4503230)
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,

grail 10-20-2011 06:29 AM

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.

rylan76 10-20-2011 08:15 AM

Quote:

Originally Posted by grail (Post 4503349)
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

grail 10-20-2011 09:08 AM

Cool ... glad we got there .. don't forget to mark as SOLVED.


All times are GMT -5. The time now is 11:14 PM.