Solved

Getting only required value using regex?

Posted on 2011-03-03
21
369 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
Question by:myselfrandhawa
  • 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
 
LVL 15

Author Comment

by:myselfrandhawa
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 15

Author Comment

by:myselfrandhawa
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 15

Author Comment

by:myselfrandhawa
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 15

Author Comment

by:myselfrandhawa
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 15

Author Comment

by:myselfrandhawa
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 74

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 74

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 74

Expert Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now