开发者

How can I make a regex to find instances of the word Project not in square brackets?

开发者 https://www.devze.com 2022-12-08 01:30 出处:网络
For example: $lang[\'Select Project\'] = \'Select Project OK\'; $lang[\'Project\'] = \'Project\'; I want to find only the instances of the word \'Project\' not contained within the square brackets

For example:

$lang['Select Project'] = 'Select Project OK';

$lang['Project'] = 'Project';

I want to find only the instances of the word 'Project' not contained within the square brackets.

I'm using ColdFusion studio's extended replace utility to do a global replace.

Any suggestions?

Code Sample Follows:

    <?php
$lang['Project Message Board']                  = 'Project Message Board';
$lang['Project']                                = 'Project';
$lang['Post Message']                           = 'Post Message';
$lang['To']                                     = 'To';
$lang['Everyone']                               = 'Everyone';
$lang['From']                                   = 'From';
$lang['Private Messsage']                           = 'Private Messsage';
$lang['Note: Only private message to programmer']                           = '[ Note: Please enter programmers id for private message with comma separate operator ]';
$lang['Select Project']                         = 'Select Project';
$lang['message_validation']                     = 'Message';
$lang['You must be logged in as a programmer to post messages on the Project Message Board']    = 'You must be logged in as a programmer to post messages on the Project Message Board';
$lang['Your Message Has Been Posted Successfully']                                              = 'Your message has been posted successfully';
$lang['You must be logged to post messages on the Project Message Board']                       = 'You must be logged to post messages on the Project Message Board';
$lang['You must be post project to invite programmers']                                         = 'You must be post project to invite programmers';
$lang['You must be logged to invite programmers']                                               = 'You must be logged to invite programmers';
$lang['There is no open project to Post Mail']                                                  = 'There is no open project to Post Mail';
$lang['You are开发者_运维技巧 currently logged in as']='You are currently logged in as';
$lang['Tip']='Tip: You can post programming code by placing it within [code] and [/code] tags.'; 
$lang['Submit']='Submit';
$lang['Preview']='Preview';
$lang['Hide']='Hide';
$lang['Show']='Show';
$lang['You are currently logged in as']='You are currently logged in as';


A regexp for 'Project' to the right of an equals sign would be:

/=.*Project/

a regexp that also does what you ask for, 'Project' that has no equals sign to its right would be:

/Project[^=]*$/

or a match of your example lines comes to:

/^\$lang['[^']+']\s+=\s+'Project';$/

By placing 'Project' in brackets () you can use that match in a replacement, adding the flag /g finds all occurences in the line.


Edit: Below didn't work because look-behind assertions have to be fixed-length. I am guessing that you want to do this because you want to do a global replace of "Project" with something else. In that case, borrowing rsp's idea of matching a 'Project' that is not followed by an equals sign, this should work:

/Project(?![^=]*\=)/

Here is some example code:

<?php

$str1 = "\$lang['Select Project'] = 'Select Project OK';";
$str2 = "\$lang['Project'] = 'Project';";
$str3 = "\$lang['No Project'] = 'Not Found';";
$str4 = "\$lang['Many Project'] = 'Select Project owner or Project name';";

$regex = '/Project(?![^=]*\=)/';

echo "<pre>\n";

//prints:  $lang['Select Project'] = 'Select Assignment OK';
echo preg_replace($regex, 'Assignment', $str1) . "\n";

//prints:  $lang['Project'] = 'Assignment';
echo preg_replace($regex, 'Assignment', $str2) . "\n";

//prints:  $lang['No Project'] = 'Not Found';
echo preg_replace($regex, 'Assignment', $str3) . "\n";

//prints:  $lang['Many Project'] = 'Select Assignment owner or Assignment name';
echo preg_replace($regex, 'Assignment', $str4) . "\n";

This should work:

/(?<=\=.*)Project/

That will match only the word "Project" if it appears after an equals sign. This means you could use it in a substitution too, if you want to replace "Project" on the right-hand-side with something else.


Thx for help. Not sure what is unclear? I just want to find all instances of the word 'Project' but only instances to the right of the equals sign (i.e. not included in square brackets). Hope that helps.


This actually looks like a tricky problem. Consider

[blah blah [yakkity] Project blah] Project [blah blah] [ Project

This is a parsing problem, and I don't know of any way to do it with one regex (but would be glad to learn one!). I'd probably do it procedurally, eliminating the pairs of brackets that did not contain other pairs until there were none left, then matching "Project".


While it's not clear what instances you want to find exactly, this will do:

^.+? = (.+?);

But you might consider using simple string manipulation of your language of choice.

edit

^.+?=.+?(Project).+?;$

will only match lines that have string Project after the equal sign.


[^\[]'[^'\[\]]+'[^\]] seems to accomplish what you want!

This one: [^\[]'[^'\[\]]*Project[^'\[\]]*' will find all strings, not inside of the file that are contained in quotes, and contain the word project.

Another edit: [^\[]'(?<ProjectString>[^'\[\]]*Project[^'\[\]]*)'[^\]] This one matches the string, and returns it as the group "ProjectString". Any regex library should be able to pull that out sufficiently.

0

精彩评论

暂无评论...
验证码 换一张
取 消