Solved

Getting only required value using regex?

Posted on 2011-03-03
21
375 Views
Last Modified: 2012-05-11
I have the following date and i just to extract the following from the string

25

from
+25 15:12:00.000000

Please guide
0
Comment
  • 9
  • 5
  • 3
  • +2
21 Comments
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35025035
You can use DAY() function ?
0
 
LVL 18

Accepted Solution

by:
Plucka earned 125 total points
ID: 35025045
<cfset data = "+25 15:12:00.000000" />
<cfset result = listFirst(data, " +") />

NOTE: thats a SPACE and a + in the quotes on the second line
0
 
LVL 11

Assisted Solution

by:Brijesh Chauhan
Brijesh Chauhan earned 125 total points
ID: 35025061
<cfset dayString = '+25 15:12:00.000000' />

<cfset dayString = replace(dayString,'+','','ALL') />

<cfset date = left(daystring,2) />

<cfoutput> #date# </cfoutput>

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 35025139
that wil alos help if the value after +1056 blah blah

right plucka

it will extract 1056
0
 
LVL 18

Expert Comment

by:Plucka
ID: 35025224
yes, it treats the string as a list, uses either SPACE or + as a seperator and gets the first item.
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35025262
you didn't specify it can be more than 2 characters.. here will work with dynamic


<cfset dayString = '+25234 15:12:00.000000' />

<cfset dayString = replace(dayString,'+','','ALL') />

<cfset count = find(' ','#dayString#') />

<cfset date = left(daystring,count) />

<cfoutput> #date# </cfoutput>

Open in new window

0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35025379
Explanation.. remove the + sign, get the blank where it occurs, then get count characters from the string..

<cfset dayString = '+1056 blah blah 15:12:00.000000' />

<cfset dayString = replace(dayString,'+','','ALL') />

<cfset count = find(' ','#dayString#') />

<cfset date = left(daystring,count) />

<cfoutput> #date# </cfoutput>

Open in new window


this will output date as 1056 ..... Test it out..
0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 35025793
i want that beforethe .dot sign it shuld just pick the chracters

leave the + n space, i am sorry for this

19.3999999999999
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35025826
didn't get what you want to extract ?? above example is not very clear...
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35025830
<cfset dayString = '+1056 blah blah 15:12:00.000000' />

<cfset dayString = replace(dayString,'+','','ALL') />

<cfset count = find('.','#dayString#') />

<cfset count = count - 1 />

<cfset date = left(daystring,count) />

<cfoutput> #date# </cfoutput>

Open in new window


would return '1056 blah blah 15:12:00' .. is that what you are looking for ????
0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 35025883
Guys, Plesae levae off the + and the space

just wanna get

19

from
19.27364582364782647826472634862347826834724
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35025905
Here you go... this is dynamic

<cfset dayString = '19.27364582364782647826472634862347826834724' />
<cfset count = find('.','#dayString#') />
<cfset count = count - 1 />
<cfset date = left(daystring,count) />
<cfoutput> #date# </cfoutput>

Open in new window

0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 35025998
Thanks, but nw i am getting this issue

The 2 parameter of the Left function, which is now -1, must be a positive integer
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35026013
what is the string that you are checking ??? that will only come if you do not have a "." in your string...
0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 35026022
i am checking the same above u are telling me same scenario
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35026045
You are checking the below code ??? it works correctly for me.. even I changed the string to 19273645823.64782647826472634862347826834724 and 1927364582364.782647826472634862347826834724, the only way I get the error you have posted is when there is no period in string, in that case count would be zero and count - 1 would be -1 and that gives you the error....

<cfset dayString = '19.27364582364782647826472634862347826834724' />
<cfset count = find('.','#dayString#') />
<cfset count = count - 1 />
<cfset date = left(daystring,count) />
<cfoutput> #date# </cfoutput>

Open in new window


can you post the portion of code that you are using ??
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 125 total points
ID: 35026988
Here's my contribution.

Given: +25 15:12:00.000000

<CFSet data = '+25 15:12:00.000000' />
<CFSet extracted=REReplace("#data#", "^.*?\+(\d+).*$", "\1") />
<CFDump var=#extracted# label="href" />

Open in new window



Given: 19.27364582364782647826472634862347826834724

<CFSet data = '19.27364582364782647826472634862347826834724' />
<CFSet extracted=REReplace("#data#", "^.*?(\d+)\..*$", "\1") />
<CFDump var=#extracted# label="href" />

Open in new window

0
 
LVL 39

Assisted Solution

by:gdemaria
gdemaria earned 125 total points
ID: 35027124

There is a simplier way, you just need to use the list functions.

This one line will do it for you...

<cfoutput>#listFirst(data,".+- ")#</cfoutput>


Try the code below...
<cfset data = '-19.27364582364782647826472634862347826834724'>

<cfoutput>#listFirst(data,".+- ")#</cfoutput>

<cfset data = '+25 15:12:00.000000' />

<cfoutput>#listFirst(data,".+- ")#</cfoutput>

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35027726
@gdemaria

That was mentioned above  http:#35025045 .   = )
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35027787
> That was mentioned above  

Almost, but not quite.   The author changed (or added to) his requirements to also take the absolute value of a negative value and to truncate at the decimal point.

I added those two additions to cover all requirements.  

The list function is very simple and reliable.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35028275
Point taken  : )
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question