Jump to content


an interesting problem about RegExReplace


  • Please log in to reply
13 replies to this topic

#1 RegExReplace

RegExReplace
  • Guests

Posted 18 October 2012 - 05:39 AM

an interesting problem about RegExReplace, not use loop, please
from
str1 := "dab/c/dco"
str2 := "dabc|d|co"
i wanna get
newstr := "dab/c|/d|co"


#2 Sjc1000

Sjc1000
  • Members
  • 462 posts

Posted 18 October 2012 - 05:55 AM

Hi RegExReplace, this will match both of them.

str1 := "dabc|d|co"

RegExMatch(str1, "dab(c\||/c/)(d\||d)co", _)

MsgBox, %_%


EDIT: sorry i just re-read the question, ill work on what you want

#3 RegExReplace

RegExReplace
  • Guests

Posted 18 October 2012 - 08:14 AM

thanks, use RegExReplace, not use loop, please

#4 Guests

  • Guests

Posted 18 October 2012 - 08:17 AM

thanks, use RegExReplace, not use loop, please

I'm starting to think you are a bot - That is your standard response to everything.
Learn to solve your problems with functions.

#5 Sjc1000

Sjc1000
  • Members
  • 462 posts

Posted 18 October 2012 - 08:51 AM

I think this might be a little beyond me, hopefully someone else will have the answer. Sorry :(

#6 CodeKiller

CodeKiller
  • Members
  • 2066 posts

Posted 18 October 2012 - 11:17 AM

It's not related to RegEx...
You can't use 2 string to form a single string with RegEx in a single command... >_>
You can only do that checking all subpart of the 2 strings and apply hand-made rules... It's impossible to create a single shot command for that, even in function it would be complicated.
In you example :

str1 := "dab/c/dco"
str2 := "dabc|d|co"
newstr := "dab/c|/d|co"

When the string containt /c you must take it, then if yout strings contain a "|" and a "/" for the same char it gets the "|" then the char, then adds the "/"...

Impossible to solve in in a quick way...

#7 RegExReplace

RegExReplace
  • Guests

Posted 18 October 2012 - 11:38 AM

thanks, is it possible to make it with RegEx in two more command?

#8 sinkfaze

sinkfaze
  • Moderators
  • 6079 posts

Posted 18 October 2012 - 12:03 PM

[color=#000080]str :=	"dabc|d|co"

MsgBox %	RegExReplace(str,"(?:c|d)\|","/$0")

[color=#00BF00]; or[/color]

str :=	"dab/c/dco"

MsgBox %	RegExReplace(str,"/(?:c|d)","$0|")[/color]


#9 RegExReplace

RegExReplace
  • Guests

Posted 18 October 2012 - 12:28 PM

[color=#000080]str :=	"dabc|d|co"
MsgBox %	RegExReplace(str,"(?:c|d)\|","/$0")
[color=#00BF00]; or[/color]
str :=	"dab/c/dco"
MsgBox %	RegExReplace(str,"/(?:c|d)","$0|")[/color]

thanks, nice, but my example isnt clear enough, i change it like
str1 := "dabc/dco/c"
str2 := "dabc|d|coc"
newstr := "dabc|/d|co/c"


#10 Guests

  • Guests

Posted 18 October 2012 - 12:34 PM

thanks, nice, but my example isnt clear enough, i change it like

That is because you are always too vague.

1. You need to start explaining it better
2. Stop complaining about loops

It is obvious you have two strings and you want one regexp to produce a newstr according to the patterns in str1 and str2. You can cry "no loops" all you want but it is never going to happen in this case. You need to write your own functions that will analyse the two strings and combines them into a newstr. And yes that solution will include a loop one way or another.

#11 CodeKiller

CodeKiller
  • Members
  • 2066 posts

Posted 18 October 2012 - 12:37 PM

... Someone has a rope and a chair ??? >_>

#12 RegExReplace

RegExReplace
  • Guests

Posted 18 October 2012 - 01:01 PM

str1 := "dabc/dco/c"
str2 := "dabc|d|coc"
newstr := "dabc|/d|co/c"
str1 consists of "dabcdcoc" and "/".
str2 consists of "dabcdcoc" and "|".
newstr consists of "dabcdcoc", "|" and "/". "|" before "/".

i mean two more regexp to make it, not just one regexp to make it

#13 Guests

  • Guests

Posted 18 October 2012 - 01:25 PM

I mean two more regexp to make it, not just one regexp to make it

Can not be done without analysing the strings...

#14 sinkfaze

sinkfaze
  • Moderators
  • 6079 posts

Posted 18 October 2012 - 04:17 PM

Guest is correct.

This is probably as easy as it's going to get:

[color=#000080]str1 := "dabc/dco/c"
 , str2 := "dabc|d|coc"
 , sort :=	RegExReplace(RegExReplace(str1,"\W"),"\w(?=\w)","$0,")	[color=#00BF00];sort out only the letters in the sequence of 'str1'[/color]
Sort, sort, U D`,	[color=#00BF00]; remove all duplicate letters[/color]
Loop, parse, sort, `,	[color=#00BF00]; parse through each letter in 'str1'[/color]
	if	InStr(str1,"/" A_LoopField)	[color=#00BF00]; if the letter is in 'str1' with a preceding slash[/color]
		While	Pos :=	InStr(str1,A_LoopField,0,1,A_Index)	[color=#00BF00]; find out which occurrence of the letter it is[/color]
			if	SubStr(str1,Pos-1,2) = "/" A_LoopField
				i :=	InStr(str2,A_LoopField,0,1,A_Index), str2 :=	SubStr(str2,1,i-1) "/" SubStr(str2,i)	[color=#00BF00]; splice the slash into 'str2' based on the character's occurrence in 'str1'[/color]
MsgBox %	str2[/color]