Solved

REGEXEXTRACT to extract a subsection of text

Posted on 2014-10-04
10
340 Views
Last Modified: 2015-03-02
Hiya, I recently came across the REGEXEXTRACT function in google docs but there is no documentation. I think it will do what i want though, that is to split a windows file path into parts based on searches eg:

C:\user\Dropbox\property\maintenance & repairs\open\oddj smp1 slight leak through new window (normal).jpg

extract "open" with the rule: the text between the last two backslashes
extract "oddj" with the rule: from the last backslash to the next space
extract "smp1" with the rule: from "oddj" until the second space
extract  "slight leak through new window" with the rule from "smp1" until the full stop (or space before if there is one)
extract  "normal" with the rule text between the last set of brackets

Can anyone help with some or all of these? The rules can be bent as long as they give the correct answer in any similar example.

Thanks a million!
0
Comment
Question by:xenium
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 

Author Comment

by:xenium
ID: 40361951
0
 

Author Comment

by:xenium
ID: 40361953
Item 3 extract "smp1" , the following almost does this except the bounding conditions are included in the result, how do i remove these?

=REGEXEXTRACT(fullpath, trade & "\w*\s")

Gives result: oddj smp1
I need just: smp1

Thanks again
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 40362137
Using capture group should work:
=REGEXTRACT(fullpath, trade & "(\w*)\s")

Open in new window


It may return it in array context (not sure).  If so, you'd need to do whatever Google Docs expects to grab the first array element in scalar context.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:xenium
ID: 40362157
Great thanks, that works straight off (bar typo in function name which is REGEXEXTRACT)

I'll have a look at the rest later as I have to dash now, but looks like that would work for a few of the points above. Meanwhile if you have any tips on any that maybe trickier feel free to post. cheers
0
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 500 total points
ID: 40362229
The only one that looks like it would be possibly trickier than smp1 is the last one (capturing from the last parens).  Provided it follows the norm, you'll probably have to do this:
=REGEXEXTRACT(fullpath, ".*\(([^)]*)\)")

Open in new window


That should force it to skip as much as possible before grabbing what's inside the last set of parens.
0
 

Author Comment

by:xenium
ID: 40362809
Thanks that works a treat! I'll have a closer look tomorrow and try to wrap up the rest too.
0
 

Author Comment

by:xenium
ID: 40363413
This is powerful stuff, but i'm still struggling, could you help with the first and fourth cases too?

Maybe also some tips how to "read" such expressions. eg i think the following is the case for the last example, can you confirm/correct, also how the logic ties together..

Look for any character except closed parentheses:
[^)]
Return all characters except closed  parentheses:
([^)]*)
Look for any number of characters:
.*
Look for open/closed parentheses:
\(
\)

Open in new window


Thanks again!
0
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 500 total points
ID: 40364289
You have it correct for the last one.

The first case is basically the same as the last case:
=REGEXEXTRACT(fullpath, ".*\\([^\\]*)\\")

Open in new window


The fourth case is tricky unless you can guarantee there will not be a paren or period in it.  If you can guarantee that, it becomes:
=REGEXEXTRACT(fullpath, trade & "\w*\s([^.\(]+)")

Open in new window


If you can't guarantee no paren or period, the simplest way is to capture the previous and next result and obtain it that way.  For example, if rule3 and rule5 contain those results then it would be:
=REGEXEXTRACT(fullpath, rule3 & "\w*(.*)\w*\(" & rule5)

Open in new window


If there may not be a rule5 match, it gets even trickier.
0
 

Author Comment

by:xenium
ID: 40364654
Thanks a lot for your help. That does the job for now. If i need to dig further I'll repost new questions.
0
 

Author Closing Comment

by:xenium
ID: 40364656
Many thanks for your help!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Google Forms is a great platform to easily create polls, questionnaires and surveys. The service allows you to receive data in real-time and is a simple way to conduct large-scale surveys that can be distributed to multiple people.   The platform …
Learn about cloud computing and its benefits for small business owners.
By using UNIQUE function in Google Sheets, you can get around removing duplicates like in Microsoft Excel.
This Micro Tutorial will demonstrate how to use comments in Google Docs. You will learn how to create, edit, and delete a comment.

689 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