[RESOLVED] Regular expression to find Tokens in HTML page
Hey there,
im trying to create a regular expression to replace Tokens on an HTML page.
each token will begin with |~ and end with ~|
eg1: |~TOKEN1~|
eg2: |~MY TOKEN~|
eg2: |~Another_Token~|
these can lie anywhere in HTML page, so all i care about is whats between the start and end delimiters
im struggling with creating a pattern.
what i have tried is "|~[A-Za-z0-9_]~| but it doesnt work for me..
any help is appreciated
Re: Regular expression to find Tokens in HTML page
try this:
vb Code:
Dim rx As New Regex("(?<=\|~)(\d|\w| )*(?=~\|)")
Re: Regular expression to find Tokens in HTML page
Slight modification to paul's. The whitespace needs to be specified with \s.
Also doing a named capture.
(?<=\|~)(?<tokengesture>(\d|\w|\s)*)(?=~\|)
Re: Regular expression to find Tokens in HTML page
can you please explain the (?<=\\|~) part of the pattern,
i need to replace the entire token, so |~TOKEN~| -> token replacement
Re: Regular expression to find Tokens in HTML page
try this:
vb Code:
Dim testStr As String = "eg2: |~Another_Token~|"
MsgBox(Regex.Replace(testStr, "\|~(\d|\w|\s)*~\|", "new String value"))
Re: Regular expression to find Tokens in HTML page
yeah paul, i had changed it to that and it worked, sweet cheers mate...
but one last question why do u put the \ between the ~ and |, when i first changed it i done like \~| what does this backspace mean
Re: Regular expression to find Tokens in HTML page
its an escape character. it means to treat the | as a literal character. the | character has special meaning in regex.