Skip to main content
gokul1242
Inspiring
April 22, 2013
Answered

How to fetch required values from a string ?

  • April 22, 2013
  • 1 reply
  • 946 views

Hi ,

I need to fetch a particular value from a string,

eg:

input :

abc def def test|test2|test3

my required output should be:

test|test2|test3

How to go about this using any function or any other alternate solution.

Kindly advice !!!

This topic has been closed for replies.
Correct answer duncancumming

ok, so we probably need to use a regular expression then.  Something like this perhaps:

Find: ^[^ ]*? [^ ]*? [^ ]*? (.*)$

Replace with: \1

This worked for me:

<cfset yourString = "abc def def test|test2|test3">

<cfoutput>

#reReplaceNoCase(yourString, "^[^ ]* [^ ]* [^ ]* (.*)$", "\1")#

</cfoutput>

<br />

<cfset yourString = "abc def def test|test 2|test3">

<cfoutput>

#reReplaceNoCase(yourString, "^[^ ]* [^ ]* [^ ]* (.*)$", "\1")#

</cfoutput>

1 reply

Participating Frequently
April 22, 2013

Your requirements are a little bit unclear.  But it seems to me that you just want to get the bit at the end of the string, following the last space?  You could just use listLast(), specifying a space as the delimiter.

<cfset yourString = "abc def def test|test2|test3">

<cfoutput>

#listLast(yourString, " ")#

</cfoutput>

gokul1242
gokul1242Author
Inspiring
April 22, 2013

hi

actually my requirement is to exclude the string values till first three spaces and print the remaining.

ie.

input :

abc def def test|test2|test3

i need to exclude "abc def def" and print the remaning test|test2|test3

but listLast will not work for some cases like

input :

abc def def test|test 2|test3

this prints

2|test3

duncancummingCorrect answer
Participating Frequently
April 22, 2013

ok, so we probably need to use a regular expression then.  Something like this perhaps:

Find: ^[^ ]*? [^ ]*? [^ ]*? (.*)$

Replace with: \1

This worked for me:

<cfset yourString = "abc def def test|test2|test3">

<cfoutput>

#reReplaceNoCase(yourString, "^[^ ]* [^ ]* [^ ]* (.*)$", "\1")#

</cfoutput>

<br />

<cfset yourString = "abc def def test|test 2|test3">

<cfoutput>

#reReplaceNoCase(yourString, "^[^ ]* [^ ]* [^ ]* (.*)$", "\1")#

</cfoutput>