Link to home
Start Free TrialLog in
Avatar of Delphi_developer

asked on

A little help on regular expressions - TRegExpr


in this question:

I got the solution for my problem with regular expressions:

"I need to replace part of an expression with new text, and leave the rest of expression unchanged.

For example, in expressions, like : ' myFunc(0)='XML' '  I need to replace myFunc with newProcedure, and add the rest of the expression:

myFunc(0)='XML'  ->  newProcedure(0)='XML'

Expression can be in various versions, all can include unknown number of spaces! :

myFunc    (0)='XML'
myFunc(0)   =  'XML'
myFunc(0         )= 'XML'
myFunc( 0 )= 'XML'

Now I have another question:

How can I make sure it will only replace strings starting 'myFunc...' and not any others, like:


but I still need it to convert these examples:


Well, for now it would be good to just omit 'n' as 'nmyFunc', because 'nmyFunc' is another function.

I tried with :

'[^n]myFunc...' so that 'nmyFunc' is not converted, but this syntax doesn't work.

Please, help.

Avatar of 2266180
Flag of United States of America image

well, a function name must start with _ or any alpha character
so "[^_a-zA-Z]*myFunc" will only match myFunc, 0myFunc, -myFunc, +myfunc, and will not match _myFunc, abcmyFunc, etc

basically, you just need ot include all possibilities in your regular expression or create several regular expressions.
This worked for me.


Let me know if you need more.
Sorry, my example finds +myFunc, -myFunc, (myFunc but not myFunc.  Looking into it more.
>> so "[^_a-zA-Z]*myFunc" will only match myFunc, 0myFunc,

btw, if you don't want to match 0myFunc then all you need to do is ues

The list I am testing on is this.


this is my search string.
[ ]@[-+(]*myfunc

the first part finds one or more spaces
[ ]@

the second part is zero or more of the characters -+(

the last part is myFunc

Now, if this does not work for all of your instances you should be able to taylor it.  Let me know if you need more help.
Avatar of Delphi_developer



well, I couldn't make it work. Perhaps I'm doing something wrong...

Here is the original expression:

(myFunc)([ ]*)(\()([ ]*)([0-9])([ ]*)(\))([ ]*)(=)([ ]*)('XML')

I have 'myFunc' in brackets, so that it grouped. I tried all yours samples, put before and after '(', but no luck.

Here are a couple of example what needs to be doing OK:

"A=B AND myFunc(0)='XML' " ->  "A=B AND newProcedure(0)='XML' "

AND can be replaced with + - or ( and space, if there's any other character then it shouldn't convert:

"A=B AND _abc_myFunc(0)='XML' " ->  "A=B AND _abc_myFunc(0)='XML' "

Also just:

"myFunc(0)='XML'" shoudl work fine.

I use it like this:

edit2.text:='A=B AND myFunc(0)='XML'';

label1.Caption := vExpr.Replace(edit2.Text, 'newProcedure$3$5$7$9$11', true);

I hope this will help you help me.

Thanx a lot

I would advice
ow's this:

    vExpr.Expression:='(.*[ \x2a\x2b-/\x28]+)([^_a-zA-Z0-9]*?myFunc)(.*)';
    label1.Caption := vExpr.Replace(edit2.Text, '$1newProcedure$3', true);

\xDD is the char code of a character, where DD is the hexa code, so \x2a is "*".
@ciuly: it works, except for example where it is just:


it doesn't work.

I'm afraid I missed the TRegExpr at the top.  I was trying to show examples using Delphi's own find operation (in the IDE) using regular expressions, not the TRegExpr.  I do not have the TRegExpr, but I suspect that there are differences in the regular expression language it uses.  Sorry for the confusion.
So You  ignore my post it's too simple?
In this version You've got it better grouped
how about now?:

>> myFunc(0)='XML'

    vExpr.Expression:='^(.*[ \x2a\x2b-/\x28]+)?([^_a-zA-Z0-9]*?myFunc)(.*)';

(notice the "^" at begining of expression)

btw: mokules suggestion looks very promising.
@mokule: your's is great, short and neat. But it needs a little correction:

"1+myFunc(0)='XML'" -> "1newProcedure(0)='XML'" ...

it seems it cuts the first character before myFunc.

@ciuly: as I tested it, it seems workable solution. Although more complicated than mokule's.

I'll do some more testing with all sorts of possibilities, meanwhile if mokule can provide a 'fix' for his solution, that be great.

Thanx, I'll let you know how test goes.
I'm not sure how do You perform replace exactly, but for string
my mask gives first group '+' , second 'myFunc' and third '('
so it seems that you shouldn't loose +

if I use:

label1.Caption := vExpr.Replace(edit2.Text, '$1newProcedure$3', true); //   check $1

then, it is OK, it doesn't eat the first char. But then this one doesn't work:


any suggestions?
ok try this one
Avatar of mokule
Flag of Poland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
 a quick test shows it works, both last versions. It's getting late here, so I need to finish for today. I'll do a thorough test tomorrow and let you know the results.

Very simple, but works. It goes very nice with now full expression:

(\bmyFunc\b)([ ]*)(\()([ ]*)([0-9])([ ]*)(\))([ ]*)(=)([ ]*)('XML')

Thank you
and I didn't get points because? ... well, it doesn't matter anymore. when you get 2 working solutions, you accept one and assist the other. keep that in mind for the future.

in the meanwhile, you made it 17th on my blacklist. which basically means I will no longer answer any of your questions.


PS: no need to answer. I've unsubscribed from this question.

blacklisted because I didn't know how (didn't know I even could) to split points...? Well, you could've checked my profile and you'd see that I'm more a beginner here, and I never split the points, yet.

Well, now I now:

From Help::

Can I split the points?

Each comment box has a button that says Accept Multiple Solutions. Click that, and you will see a page that allows you to assign points to any of the comments in the thread. There is a grade box at the bottom of the page.

Note: The total of the point splits must equal the original amount you assigned to the question, and no comment can receive fewer than 20 points. The Comment that was posted first is the Accepted Solution, and the rest of the comments are Accepted Solutions.