Sign in to follow this  
Followers 0
iCode

RegEx help: StringRegExpReplace and Conditional Patterns

9 posts in this topic

Don't know if this is possible, but given the example strings:

1A

A

I want to find the string:

(d)?([A-Z])

and if the digit is not present, then i want to add it using StringRegExpReplace. So given the example strings, the result in both cases should be:

1A

This is trivial to do using a method other than StringRegExpReplace, but i want to see if this can be done this way... just because. It seems like the "Conditional patterns" section of the help doc for StringRegExp might hold the key, but i have not been able to get it to work. Here are a couple match patterns i tried:

(d)?(?(1)|1)([A-Z])

(d)?(?(1)$1|1)([A-Z])

(d)?(?(1)g1|1)([A-Z])

And replace pattern is a generic $1$2$3


FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Share this post


Link to post
Share on other sites



$s = "first string is 1A and second is A, that's it ?"

Msgbox(0,"", StringRegExpReplace($s, "((?<!\d)[A-Z])", '1$1') )

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

1A

1

 

Search:  b(?:[0-9]+|([A-Z]))

Replace: 1$1

Edited by Jury

Share this post


Link to post
Share on other sites

Jury,

$s = "first string is 2A and second is A, that's it ?"

The existing digit is always replaced by "1"  :)

Share this post


Link to post
Share on other sites

Sorry, the "1" could be anything and the "A" could be anything. I just made the example as simple as i could.

What i'm actually trying to do is find text links in a string and wrap them in HTML to make hyperlinks. So the string could be something like:

go to ex.com but don't go to http://ex2.de or https://ex3.eu

The result needs to be:

go to <a href="ex.com">ex.com</a> but don't go to <a href="http://ex2.de">http://ex2.de</a> or <a href="https://ex3.eu">https://ex3.eu</a>

The problem i'm having with StringRegExpReplace is that, if the https? is present, then the it gets doubled. I need to add it only if it is not present using only StringRegExpReplace. If this can't be done i can do it another way, but i'd like to see if it can.


FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Share this post


Link to post
Share on other sites

Aha, the problem is mucho different  :)

$s = "go to ex.com but don't go to http://ex2.de or https://ex3.eu"

Msgbox(0,"", StringRegExpReplace($s, "(\S+\.\S+)", '<a href="$1">$1</a>') )

Share this post


Link to post
Share on other sites

that doesn't prefix the string with "http" if it's missing, so it is not a valid hyperlink :)

in order to do this with only StringRegExpReplace, i am pretty sure the "Conditional patterns" for StrinRegExp needs to be utilized


FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Share this post


Link to post
Share on other sites

$s = "go to ex.com but don't go to http://ex2.de or https://ex3.eu"

Msgbox(0,"", StringRegExpReplace($s, "(?:(http)(s?)(://))?(\S+\.[a-z]+)", '<a href="http$2://$4">$1$2$3$4</a>') )

Weird... how do you guess that the missing prefix is http or https ?

Share this post


Link to post
Share on other sites

it's just an assumption

if you run across text like example.com within an article or whatever, http will likely work


FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Share this post


Link to post
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
Sign in to follow this  
Followers 0