variable in regex ? possible ?

jeffnco

Новичок
Регистрация
23.01.2020
Сообщения
12
Благодарностей
0
Баллы
1
Hi,
I have this regex :

C#:
(?<=(\w+\s){3})\w+\s\w+
I would like to replace the number 3 by a variable like this :
C#:
(?<=(\w+\s){{-Variable.myvar-}})\w+\s\w+
But it doesn't work with the text-processing regex
I try with quote and many things but nothing do.

I even try it in the C# code window but it doesn't work.

Have you got an idea how i can do that ?
thank you
 

nicanil

Client
Регистрация
06.03.2016
Сообщения
2 242
Благодарностей
1 820
Баллы
113
Use Variables processing=>Set Value or Text Processing=>To variable (1) to process string with variable's macros in it and to save it to a new variable. Then use created variable as a regex string in Text Processing=>Regex (2)
65760
 

VladZen

Administrator
Команда форума
Регистрация
05.11.2014
Сообщения
22 457
Благодарностей
5 913
Баллы
113
Variable macro should work in regex string.
I think the problem is in variable value when it does not work.
 

jeffnco

Новичок
Регистрация
23.01.2020
Сообщения
12
Благодарностей
0
Баллы
1
Hi,
Thank you for your answer.
I have already tried the way you say nicanil.
But i still doesn't work.
The variable is the result number of a random.
When i do with a number instead of the variabe it works, but not with variables...
don't why, i'm still going to search ..
i have zennoposter 5.45
 

Alexmd

Client
Регистрация
10.12.2018
Сообщения
1 021
Благодарностей
1 423
Баллы
113

jeffnco

Новичок
Регистрация
23.01.2020
Сообщения
12
Благодарностей
0
Баллы
1
i have also tried like this but still doesn't work :

C#:
string monNbreApparaitLien = project.Variables["varNumber"].Value;
string result = Regex.Match(project.Variables["textToModify"].Value, @"(?<=(\w+\s){varNumber})\w+\s\w+").Value.ToString();
return result;
 

jeffnco

Новичок
Регистрация
23.01.2020
Сообщения
12
Благодарностей
0
Баллы
1
Thanks Alexmd.
But i don't figure out how to use it.
thx
 

Alexmd

Client
Регистрация
10.12.2018
Сообщения
1 021
Благодарностей
1 423
Баллы
113
C#:
var regex = @"(?<=(\w+\s){"+project.Variables["myvar"].Value+"})\w+\s\w+"; 
return Regex.Match(project.Variables["textToModify"].Value, regex).Value;
 

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 812
Благодарностей
1 187
Баллы
113
@jeffnco please add a piece of template You are using. I do not see any problems there.

UPDATE:
And there is my example, a totally working one, with simply using variable straight into regex.
Added both formats of the template.
 

Вложения

jeffnco

Новичок
Регистрация
23.01.2020
Сообщения
12
Благодарностей
0
Баллы
1
Hi Lokiys,
In this example, the variable AncreLien stay empty.
I want to take 2 words randomly and replace these 2 word with the same 2 words in a link.
Thanks all for your help.
 

Вложения

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 812
Благодарностей
1 187
Баллы
113
@jeffnco The problem is Your regex. You set your random number too high.
Please check my example above everything works with smaller numbers.
Same there - in your example everything works with smaller numbers.
You can use this page for creating regexes as well as confirming if your regex is working correctly. https://regexr.com/
 

Вложения

  • Спасибо
Реакции: nicanil

jeffnco

Новичок
Регистрация
23.01.2020
Сообщения
12
Благодарностей
0
Баллы
1
Hi Lokiys,

Thank you for your answer.
You are right it is my regex which is not the one i need, and the variable in regex work correctly.
I'm going to find the right regex for my purpose.
THank you for your help.
 

jeffnco

Новичок
Регистрация
23.01.2020
Сообщения
12
Благодарностей
0
Баллы
1
actually the regex : (?<=(\w+\s){3})\w+\s\w+ take all match.
I would like only one match. I want to take randomly 2 words on a text and only two words.
I have to learn more about regex.
thx
 

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)