Iterations in Regular Expressions

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
User avatar
NordicAlbino
Junior Member
Junior Member
Posts: 58
Joined: 2007-06-09, 17:41 UTC
Location: Norway // Belgium

Iterations in Regular Expressions

Post by *NordicAlbino »

I have an issue with the regular expressions for which I cannot find a solution. More specifically: Iterators.

Take this sentence as example: (The word 'test' can be anything!)
test test test test test test test

I want a result like this:
____ test test test test test test
Thus only one occurence, the first in this case, must be replaced.

I've tried something like this:
Search: \w{1} or [tes]{1}
Replace by: _
I get this result: ____ ____ ____ ____ ____ ____ ____

WIth a Search like this: \w{1,2} I get this result: __ __ __ __ __ __ __

I've tried many many more variations; I've looked for it on the internet, but there's nothing that gives me the right result ...
Can someone please help me to put me in the right direction?

And ALSO: how can I resolve things like this?
test ____ test test test test test (only the second word must be take in account)
test test test test ____ test test (another variant)
And so on. Briefly said: I need a better understanding/explanation of the use of the iterators ...

Thanks in advance!
Gudene Må Være Gale... | #67047 - 2 User licences
User avatar
Stefan2
Power Member
Power Member
Posts: 4133
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: Iterations in Regular Expressions

Post by *Stefan2 »

Do you know how many words are in the sentence or do you have anchor point, like first or second or last space/hyphen/underline/... ?

FROM:
test test test test test test test
TO:
____ test test test test test test

Try:
Match anything (.+) until an space (\s) but non-greedy (?) to match the first white space.

So
Search: (.+?)(\s.+)
Replac: _ $2



 
User avatar
tuska
Power Member
Power Member
Posts: 3741
Joined: 2007-05-21, 12:17 UTC

Re: Iterations in Regular Expressions

Post by *tuska »

Stefan2 wrote: 2019-08-17, 11:43 UTC Replace: _ $2

Code: Select all

____$2
?
User avatar
NordicAlbino
Junior Member
Junior Member
Posts: 58
Joined: 2007-06-09, 17:41 UTC
Location: Norway // Belgium

Re: Iterations in Regular Expressions

Post by *NordicAlbino »

@ Stefan2

Thanks for your quick respons!
But ... your example does not really meet my expectations ... What I want is a more flexible command (...).

For example:
Take this string: 'word1 word2 - word3 word4 - word5 word6 - word7 word 8 - word 9'
What I want is something like this: 'word1 word2 - [word 3 word4] word5 word6 - word7 Word8 - word9'
Notice that 'word3' and 'word4' are now placed between '[...]' instead of '- ... -', and that only once. (Hence that iterator!)

--> Only the first word or combination of words that is between ' - ' (space + hyphen + space) must be taken in account.
--> The word(s) itself can be anything and of any length!
Gudene Må Være Gale... | #67047 - 2 User licences
User avatar
milo1012
Power Member
Power Member
Posts: 1158
Joined: 2012-02-02, 19:23 UTC

Re: Iterations in Regular Expressions

Post by *milo1012 »

NordicAlbino wrote: 2019-08-17, 08:35 UTC I've tried something like this:
Search: \w{1} or [tes]{1}
Replace by: _
I get this result: ____ ____ ____ ____ ____ ____ ____
This is not how one would do that, since \w or [tes] will only match one(!) character. What you want is a group, that you'll repeat ("iterate").
So basically the template for such task would be

Code: Select all

(\w+\s+){N}
where N is any number you want to repeat this.

So for your 2nd example:
NordicAlbino wrote: 2019-08-17, 13:41 UTC Take this string: 'word1 word2 - word3 word4 - word5 word6 - word7 word 8 - word 9'
What I want is something like this: 'word1 word2 - [word 3 word4] word5 word6 - word7 Word8 - word9'
So sth. like this would be possible:

Code: Select all

((\w+\s+){2}-\s+)(\w+)\s+(\w+)(.+)
Replace with

Code: Select all

$1$4 $3$5
or whatever you want to do with word3/word4.
This replacement would result in:

Code: Select all

word1 word2 - word4 word3 - word5 word6 - word7 word 8 - word 9
Sadly TC doesn't support non-capturing groups, so you have a "dummy" group in $2.

I recommend you try practicing regex with
http://regex101.com
TC plugins: PCREsearch and RegXtract
User avatar
NordicAlbino
Junior Member
Junior Member
Posts: 58
Joined: 2007-06-09, 17:41 UTC
Location: Norway // Belgium

Re: Iterations in Regular Expressions

Post by *NordicAlbino »

@ milo1012
Thank you for your advice! I'll gonna try to find it out with the link you gave me!
Gudene Må Være Gale... | #67047 - 2 User licences
User avatar
Stefan2
Power Member
Power Member
Posts: 4133
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: Iterations in Regular Expressions

Post by *Stefan2 »

NordicAlbino wrote: 2019-08-17, 13:41 UTC @ Stefan2

Thanks for your quick respons!
But ... your example does not really meet my expectations ... What I want is a more flexible command (...).
Regular Expression is a pattern matching tool. You have to find a common pattern to match.
So, RegEx is not that flexible to match different pattern in one go, ... a script could work flexible.
NordicAlbino wrote: For example:
Take this string: 'word1 word2 - word3 word4 - word5 word6 - word7 word 8 - word 9'
What I want is something like this: 'word1 word2 - [word 3 word4] word5 word6 - word7 Word8 - word9'
Notice that 'word3' and 'word4' are now placed between '[...]' instead of '- ... -', and that only once. (Hence that iterator!)

--> Only the first word or combination of words that is between ' - ' (space + hyphen + space) must be taken in account.
--> The word(s) itself can be anything and of any length!
FROM:
'word1 word2 - word3 word4 - word5 word6 - word7 word 8 - word 9'
TO:
'word1 word2 - [word 3 word4] word5 word6 - word7 Word8 - word9'

WANTED:
Take part between first and second hyphen (((the anchor points))) and put them into brackets, drop second hyphen.

TRY:
Search: (.+? - )(.+?) - (.+)
Replac: $1[$2] $3


Tip: note the '?', used here to match 'non-greedy'.


You see, you have to tell RegEx exactly what you want!
Flexibility is possible, but limited.


 
User avatar
NordicAlbino
Junior Member
Junior Member
Posts: 58
Joined: 2007-06-09, 17:41 UTC
Location: Norway // Belgium

Re: Iterations in Regular Expressions

Post by *NordicAlbino »

Stefan2 wrote: 2019-08-17, 16:52 UTC TRY:
Search: (.+? - )(.+?) - (.+)
Replac: $1[$2] $3

Tip: note the '?', used here to match 'non-greedy'.

You see, you have to tell RegEx exactly what you want!
Flexibility is possible, but limited.
@ Stefan2
Thanks for the tip!
However, so I did it already till now. But I was just looking for a more flexible manner ... a kind of formula ... with iterators/repeaters.
So, RegEx is not that flexible to match different pattern in one go, ... a script could work flexible.
An extension to the TC Rename module to imply self-written scripts would be very welcome for such matters. :)
Gudene Må Være Gale... | #67047 - 2 User licences
Post Reply