Detect a Quotation in a String

detect a quotation in a string

The regular expression is just the quote character.

echo preg_match('/"/', $string);

That will return 0 for HELLO, 1 for "HELLO" and 1 for "HELLO

Detecting Quotes in a String C++

You should use backslash to protect your quotes.

string a = str.find("\"")

will find ".

How do I search for a substring with a quotation mark inside a string in Python?

you have to add \ before quotation.

astring = "I cant figure this out"

if "can\'t" in astring:
print "found it"
else:
print "did not find it"

or you can use "find" method, for example :

if astring.find("can\'t")>-1:
print "found it"
else:
print "did not find it"

RegEx: Grabbing values between quotation marks

I've been using the following with great success:

(["'])(?:(?=(\\?))\2.)*?\1

It supports nested quotes as well.

For those who want a deeper explanation of how this works, here's an explanation from user ephemient:

([""']) match a quote; ((?=(\\?))\2.) if backslash exists, gobble it, and whether or not that happens, match a character; *? match many times (non-greedily, as to not eat the closing quote); \1 match the same quote that was use for opening.

Find characters inside quotation marks in String

var rouge = "\"Rouge One\" is an awesome movie"

var separated = rouge.components(separatedBy: "\"") // ["", "Rouge One", " is an awesome movie"]

separated.dropFirst().first

How to check for double quotes in a string object

Replace all double quotes with empty strings:

string.tr "\"", ""

How can I find quoted text in a string?

My Solution

I spent some more time thinking and came up with this solution.

Function sMineDoubleQuoteHierarchy(s As String) As String
'Check the number of quotes in the string are even - sanity check
If (Len(s) - Len(Replace(s, """", ""))) Mod 2 <> 0 Then sMineDoubleQuoteHierarchy = "Error - Odd number of quotes found in sMineDoubleQuoteHierarchy() function": Exit Function

'First thing to do is find the first and last *single* quote in the string
Dim lStart, lEnd, i As Long, fs As String
lStart = InStr(1, s, """")
lEnd = InStrRev(s, """")

'After these have been found we need to remove them.
s = Mid(s, lStart + 1, lEnd - lStart - 1)

'Start at the first character
i = 1

Do While True
'Find where the next double quote is
i = InStr(1, s, """""")

'if no double quote is found then concatenate with fs with the remainder of s
If i = 0 Then Exit Do

'Else add on the string up to the char before the ith quote
fs = fs & Left(s, i - 1)

'Replace the ith double quote with a single quote
s = Left(s, i - 1) & Replace(s, """""", """", i, 1)

'Increment by 1 (ensuring the recently converted double quote is no longer a single quote
i = i + 1
Loop

'Return fs
sMineDoubleQuoteHierarchy = s
End Function

What's going on in this solution?

The first part of the process is removing the first and last single quote from the string and returning the text between them. Then we loop through the string replacing each instance of "" and replacing it with ". Each time we do this we skip to the next character to unsure strings like """" go to "" instead of ".

Does anyone else have a better/more compact solution?


Edit

After all the suggestions in this forum I settled with this. It's got some extra error trapping to find validate nested strings.

Public Function DoubleQuoteExtract(ByVal s As String, Optional ByRef ErrorLevel As Boolean) As String
'This effectively parses the string like BASIC does by removing incidents of "" and replacing them with "

'SANITY CHECK - Check even number of quotes
Dim countQuote As Double
countQuote = Len(s) - Len(Replace(s, """", ""))

'Calculate whether or not quote hierarchy is correct:
'"..." - Is okay - Count Quotes = 2 - Count Quotes / 2 = 1
'""..."" - Is not okay - Count Quotes = 4 - Count Quotes / 2 = 2
'"""...""" - Is okay - Count Quotes = 6 - Count Quotes / 2 = 3
'""""..."""" - Is not okay - Count Quotes = 8 - Count Quotes / 2 = 4
'etc.
'Ultimately: IF CountQuotes/2 = Odd The string hierarchy is setup fine
' IF CountQuotes/2 = Even, The string Hierarchy is setup incorrectly.

Dim X As Double: X = countQuote / 2
Dim ceil As Long: ceil = Int(X) - (X - Int(X) > 0)
If ceil Mod 2 <> 0 Then sDoubleQuoteExtract = "#Error - Incorrect number of double quotes forming an incomplete hierarchy.": GoTo ErrorOccurred

'If an odd number of quotes are found then they cannot be paired correctly, thus throw error
If countQuote Mod 2 <> 0 Then sDoubleQuoteExtract = "#Error - Odd number of quotes found in sMineDoubleQuoteHierarchy() function": GoTo ErrorOccurred


'Find the next incident of single quote. Trim the string to this
s = Mid(s, InStr(1, s, String(1, Chr(34))))

'replace all instances of "" with "
s = Replace(s, String(2, Chr(34)), String(1, Chr(34)))

'Finally trim off the first and last quotes
DoubleQuoteExtract = Mid(s, 2, Len(s) - 2)
ErrorLevel = False
Exit Function
ErrorOccurred:
ErrorLevel = True
End Function

Find start and end quotes within a string

If you are sure double quotes are there as the same as what we are seeing in the subject string (balanced, no escaped double quote inside) then a regex will do the job:

echo preg_replace('~(")([^"]+)(")~', '<i class="fa fa-quote-left" aria-hidden="true"></i>\\2<i class="fa fa-quote-right" aria-hidden="true"></i>', $str);

Live demo



Related Topics



Leave a reply



Submit