HI,

I have a string like this. These are the set of phrases enclosed in double quotes:

$str="\"rna binding protein\" OR \"Transcription factor\" ";

I want to replace the string like this:

$str="\"rna|(rna) binding protein\" OR \"Transcription|(Transcription) factor\" ";

How can i replace the above string to the new string format?

I tried to use split function but could not get the desired output.

How can i get the output like this?

$str="\"rna|(rna) binding protein\" OR \"Transcription|(Transcription) factor\" ";

Regards
Vanditha

For this case no need to use the split function. regular expression is enough for that one.

$str="\"rna binding protein\" OR \"Transcription factor\" ";

# "([^ ]+)
# " => match "
# ([^ ]+) => not equal to space 1 or more time
$str=~ s{"([^ ]+)}{"$1|($1)}g;

HI,

I have a string like this. These are the set of phrases enclosed in double quotes:

$str="\"rna binding protein\" OR \"Transcription factor\" ";

You don't ever need to escape interior quotes in perl. In your case, you could use single quotes as the outer quotes or q{}. Or if you require that the outer quotes be double quotes for some reason, you can use qq{}:

my $str= qw{"rna binding protein" OR "Transcription factor"};

You can use whatever delimiters you want, so if your string happened to have curly braces inside it, you could use parenthesis:

my $str= qw("rna binding protein" OR "Transcription factor");

And if your string had both braces and parentheses, you could use #:

my $str= qw#"rna binding protein" OR "Transcription factor"#;

# "([^ ]+)

"Any white space" is represented by \s, and "not a white space" is represented by \S. So, the pattern for "not a white space, one or more times" is \S+.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.