Jump to content

Problem with quotes " and ' in regular expression


Recommended Posts

I am parsing the HTML code of a site and made this regular expression:

$array3 = StringRegExp('<a href="http://www.somesite.com/section.php" target="_blank"><span>section</span></a>','(?i)href=["']{1}[[:graph:]]{0,}section[s]{0,1}[[:graph:]]{0,}["']{1}',3)

But it shows a sintax error when closing the first bracket at '] I think it is related to the compiler mixing the quotes that surround the regular expression and the others, because when I used as a test a string that had simple quotes I had to surround it with double quotes:

$array3 = StringRegExp("<a href='http://www.somesite.com/section.php' target='_blank'><span>section</span></a>",'(?i)href=["']{1}[[:graph:]]{0,}section[s]{0,1}[[:graph:]]{0,}["']{1}',3)

But it still shows me an error at the same point. However this does compile:

$array3 = StringRegExp('<a href="http://www.somesite.com/section.php" target="_blank"><span>section</span></a>','(?i)href=["]{1}[[:graph:]]{0,}section[s]{0,1}[[:graph:]]{0,}["]{1}',3)

But I want to use both " and ' in the regular expression because there are html codes that have simple quotes and autoit does distinguish between simple and double quotes although it doesn't seem to allow me to use both, actually I think the problem is in the simple quotes inside the brackets while the regex is surrounded by simple quotes because this compiles:

$array3 = StringRegExp("<a href='http://www.somesite.com/section.php' target='_blank'><span>section</span></a>","(?i)href=[']{1}[[:graph:]]{0,}section[s]{0,1}[[:graph:]]{0,}[']{1}",3)

But I'm at the same point. Is there a way to use single quotes and double quotes in a group?? Thanks a lot for your help!

Edited by Mithrandir
Link to comment
Share on other sites

To see some more examples see the "Strings" section in Language Reference - Datatypes

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

To see an even better method use the Hex pattern \x22 (") and \x27(') or the Octal pattern equivalent \040 (") and \047 ('). For any other characters that are difficult to match you can look them up in the ascii table (help file Appendix).

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Some examples of single and double quotes in a regular expression.

#include <Array.au3>

Local $sTestString = '<a href="http://www.somesite.com/section.php" target="_blank"><span>section</span></a>' & @CRLF & _
        "<a href='http://www.somesite.com/section.php' target='_blank'><span>section</span></a>"

$array3 = StringRegExp($sTestString, '(?i)href=["' & "'" & '].*?["' & "'" & ']', 3) ; String Concatenation, or
_ArrayDisplay($array3, "String Concatenation")

$array3 = StringRegExp($sTestString, '(?i)href=[\x22\x27].*?[\x22\x27]', 3) ; Hex ASCII Code, or
_ArrayDisplay($array3, "Hex ASCII Code")

$array3 = StringRegExp($sTestString, '(?i)href=[\042\047].*?[[\042\047]', 3) ; Octal ASCII Code, or
_ArrayDisplay($array3, "Octal ASCII Code")

$array3 = StringRegExp($sTestString, '(?i)href=(["''].*?["''])', 3) ; () brackets encompass quotes, Or
_ArrayDisplay($array3, "No href=")

$array3 = StringRegExp($sTestString, "(?i)href=[""'](.*?)[""']", 3) ; () brackets capture link reference only.
_ArrayDisplay($array3, "No Quotes")
Edited by Malkey
Link to comment
Share on other sites

Thanks to everyone! I found particularly useful the tip of using the hexadecimal form of the characters suggested by GEOSoft and the string concatenation suggested by Malkey. It was also useful using StringRegExpGUI.au3 -found in \AutoIt3\Examples- that I think I saw in the signature of someone in another thread. It is useful to test regular expressions.

Edited by Mithrandir
Link to comment
Share on other sites

You will find another tool that was originally based on the StringRegExpGUI.au3 script inn my sig. I added some versatility and the ability to store SRE's in Libraries as well as a few other goodies.

it's PCRE Toolkit.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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...