Iterations in Regular Expressions
Moderators: white, Hacker, petermad, Stefan2
- NordicAlbino
- Junior Member
- Posts: 58
- Joined: 2007-06-09, 17:41 UTC
- Location: Norway // Belgium
Iterations in Regular Expressions
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!
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
Re: Iterations in Regular Expressions
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
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
Re: Iterations in Regular Expressions
Code: Select all
____$2
- NordicAlbino
- Junior Member
- Posts: 58
- Joined: 2007-06-09, 17:41 UTC
- Location: Norway // Belgium
Re: Iterations in Regular Expressions
@ 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!
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
Re: Iterations in Regular Expressions
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").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: ____ ____ ____ ____ ____ ____ ____
So basically the template for such task would be
Code: Select all
(\w+\s+){N}
So for your 2nd example:
So sth. like this would be possible: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'
Code: Select all
((\w+\s+){2}-\s+)(\w+)\s+(\w+)(.+)
Code: Select all
$1$4 $3$5
This replacement would result in:
Code: Select all
word1 word2 - word4 word3 - word5 word6 - word7 word 8 - word 9
I recommend you try practicing regex with
http://regex101.com
TC plugins: PCREsearch and RegXtract
- NordicAlbino
- Junior Member
- Posts: 58
- Joined: 2007-06-09, 17:41 UTC
- Location: Norway // Belgium
Re: Iterations in Regular Expressions
@ milo1012
Thank you for your advice! I'll gonna try to find it out with the link you gave me!
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
Re: Iterations in Regular Expressions
Regular Expression is a pattern matching tool. You have to find a common pattern to match.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 (...).
So, RegEx is not that flexible to match different pattern in one go, ... a script could work flexible.
FROM: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!
'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.
- NordicAlbino
- Junior Member
- Posts: 58
- Joined: 2007-06-09, 17:41 UTC
- Location: Norway // Belgium
Re: Iterations in Regular Expressions
@ 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.
An extension to the TC Rename module to imply self-written scripts would be very welcome for such matters.So, RegEx is not that flexible to match different pattern in one go, ... a script could work flexible.
Gudene Må Være Gale... | #67047 - 2 User licences