Jump to content

StringRegExpReplace match problem


Recommended Posts

https://regex101.com/r/8E2MuB/1

As you can see here, there is no problem with matching, but when StringRegExpReplace is applied all matches are incorrect.
Here I want to change the 6-digit value in the CODE2 tag.

$str = "<$test.123456> ;;CODE1" & @CRLF & _
"<$test.123456> ;;CODE2" & @CRLF & _
"<$test.123456> ;;CODE3"

ConsoleWrite(StringRegExpReplace($str, "([^<])?\$test\.(\d{6})[(^>)]\h;;CODE2", "000000") & @CRLF)
ConsoleWrite(StringRegExpReplace($str, "([^<])?\$test\.(\d{6})([^>])\h;;CODE2", "000000") & @CRLF)
ConsoleWrite(StringRegExpReplace($str, "<\$test\.(\d{6})>(?=\h;;CODE2)", "000000") & @CRLF)

 

Link to comment
Share on other sites

  • Developers

You mean something like this?:

ConsoleWrite(StringRegExpReplace($str, "(<\$test)\.\d{6}(>\h;;CODE2)", "$1.000000$2") & @CRLF& @CRLF)

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers
1 minute ago, youtuber said:

Yes that's what I want, but can I add a variable there?

You really need to think about these things a little longer!  You want to concatenate the content of that variable into the result string!
Something like this?

$var = "000000"
ConsoleWrite(StringRegExpReplace($str, "(<\$test)\.\d{6}(>\h;;CODE2)", "$1." & $var & "$2") & @CRLF & @CRLF)

"Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Oh yes I never thought of separating the variable, sorry :unsure:

Thank you I want to use your jos pattern for StringRegExp too. Is there a solution for that too?

Sample:

#include <array.au3>

$str = "<$test.123456> ;;CODE1" & @CRLF & _
"<$test.123456> ;;CODE2" & @CRLF & _
"<$test.123456> ;;CODE3"

$RegExp = StringRegExp($str, "(<\$test\.)\d{6}(>\h;;CODE2)",3)
If IsArray($RegExp) Then
    _ArrayDisplay($RegExp)
EndIf

https://regex101.com/r/BR75fa/1

Group 1    <$test.
Group 2    > ;;CODE2

It would be nice to get that 6-digit number from the previous and next grouping

 

Link to comment
Share on other sites

  • Developers

Put in brackets what you want to capture! Your regex101 clearly shows what you are capturing.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...