How to Extract a String from Double Quotes

Extract a string between double quotes

Provided there are no nested quotes:

re.findall(r'"([^"]*)"', inputString)

Demo:

>>> import re
>>> inputString = 'According to some, dreams express "profound aspects of personality" (Foulkes 184), though others disagree.'
>>> re.findall(r'"([^"]*)"', inputString)
['profound aspects of personality']

How to extract a string from double quotes?

As long as the format stays the same you can do this using a regular expression. "([^"]+)" will match the pattern

  • Double-quote
  • At least one non-double-quote
  • Double-quote

The brackets around the [^"]+ means that that portion will be returned as a separate group.

<?php

$str = 'This is a text, "Your Balance left $0.10", End 0';

//forward slashes are the start and end delimeters
//third parameter is the array we want to fill with matches
if (preg_match('/"([^"]+)"/', $str, $m)) {
print $m[1];
} else {
//preg_match returns the number of matches found,
//so if here didn't match pattern
}

//output: Your Balance left $0.10

How to extract string between double quotes in java?

This code will replace all quotes with one space and save quotes without brackets in the list:

public static void main(String[] args) {    
String str = "According to some, dreams express \"profound aspects of personality\" (Foulkes 184), though \"others disagree\" but truth is.";
Pattern pattern = Pattern.compile("\".*?\"");
Matcher matcher = pattern.matcher(str);

List<String> quotes = new ArrayList<>();
StringBuffer buffer = new StringBuffer();

while (matcher.find()) {
String quote = matcher.group();
int length = quote.length();
quotes.add(quote.substring(1, length - 1));
matcher.appendReplacement(buffer, " ");
}
matcher.appendTail(buffer);

System.out.println(buffer.toString());
System.out.println(quotes);
}

This solution needs some additional fixes depends on existence of empty brackets in a text, but it work in your case.

Output:

According to some, dreams express (Foulkes 184), though but truth is.

[profound aspects of personality, others disagree]

Extract text between quotation using regex python

If you want to extract some substring out of a string, you can go for re.search.

Demo:

import re

str_list = ['"abc"', '"ABC. XYZ"', '"1 - 2 - 3"']

for str in str_list:
search_str = re.search('"(.+?)"', str)
if search_str:
print(search_str.group(1))

Output:

abc
ABC. XYZ
1 - 2 - 3

How to Extract the string between double quotes having newline embedded in between the string?

You're using the wrong modifier on your regex. The /m treats the string has multiple lines where you actually want to use /s which changes "." to match any character including \n.

Changing this won't actually get you the output you want because you're repeatedly applying the transformation and it will delete any past quoted portions too. You want to also use the /g modifier which will find all possible matches and then only apply the regex the once.

use strict;

my $whole_text="\"Ankit Stackoverflow is \n awesome\" \"a\" asd asd \"he\nllo\"\n";

$whole_text =~ s/(.*?)"(.*?)"(.*?)/$2/sg;

And then if you want to get rid of the \n you'd also need.

$whole_text =~ s/\n//sg;

Regex to extract double quotes and string in quotes R

The "(.+?)" pattern matches ", then any char other than line break chars, as few as possible, up to the closest (leftmost) ". It means it can match whitespaces, too, and thus matches "play a constructive and positive role" and "active and hectic reception".

To match a streak of non-whitespace chars in-between double quotes, you need to use

stri_extract_all_regex(df$text, '"\\S+"')

The "\S+" pattern matches ", then 1 or more non-whitespace chars, and then a closing ".

See the regex demo.

If you only want to match word chars (letters, digits, _) in between double quotes, use

'"\\w+"'

See another regex demo.

To match curly quotes, use '["“]\\S+["”]' regex:

> res <- stri_extract_all_regex(test, '["“]\\S+["”]')
> unlist(res)
[1] "\"Rohingya\"" "\"Bengali\"" "\u0093Rohingya\u0094"
[4] "\u0093Bengalis\u0094"

And if you need to "normalize" the double quotes, use

> gsub("[“”]", '"', unlist(res))
[1] "\"Rohingya\"" "\"Bengali\"" "\"Rohingya\"" "\"Bengalis\""

Regex - extract string between double quotes where line starts with a keyword

If you are using Javascript, and the positive lookbehind is supported (you don't need the multiline flag, only the global flag)

(?<=classes!\((?:"[^"]*",\s*)*")[^"]*(?=")
  • (?<= Positive lookbehind to assert to the left
    • classes!\( Match classes!(
    • (?:"[^"]*",\s*)* Match optional repetitions of "...",
    • " Match a double quote
  • ) Close lookbehind
  • [^"]* Match optional chars other than "
  • (?=") Assert " to the right

Regex demo

const regex = /(?<=classes!\((?:"[^"]*",\s*)*")[^"]*(?=")/g;
const str = `classes!("text-xl", "text-blue-500")
String::from("test")
classes!("text-sm")
`;
console.log(str.match(regex))

Extract string from between quotations

>>> import re
>>> re.findall('"([^"]*)"', 'SetVariables "a" "b" "c" ')
['a', 'b', 'c']


Related Topics



Leave a reply



Submit