1

I have a file in this path

D:\ebook\comic\Author,_Title_of_Book_(http://google.com).cbz

I need to separate the string using regex and get Author, Title, and URL. The text output should be

Author
Title_of_Book
http://google.com

So far i have

([^\\]+)\.[^\\]+$

Which will get me the file only.

4
  • 4
    Does it have to be regex? In most languages this would probably be easier without it. Commented Jun 13, 2016 at 1:21
  • Yeah, it has to be regex. Commented Jun 13, 2016 at 1:31
  • Can you explain the regex requirement? I'm not sure you're going to be able to cause three lines of output to happen with underscores converted to spaces all inside a single regular expression. Maybe you can define the constraints a little better. Commented Jun 13, 2016 at 1:39
  • I made some changes. All i want are Author, Title_of_Book, google.com selected Commented Jun 13, 2016 at 1:54

2 Answers 2

2

Description

^[^,]*\\([^,]*),_([^(]*)_\(([^)]*)\)

Regular expression visualization

This regular expression will do the following:

  • separate the string using regex and get Author, Title, and URL

Example

Live Demo

https://regex101.com/r/aB8zQ6/1

Sample text

D:\ebook\comic\Author,_Title_of_Book_(http://google.com).cbz

Sample Matches

MATCH 1
1.  [15-21] `Author`
2.  [23-36] `Title_of_Book`
3.  [38-55] `http://google.com`

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  [^,]*                    any character except: ',' (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  \\                       '\'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [^,]*                    any character except: ',' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  ,_                       ',_'
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    [^(]*                    any character except: '(' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  _                        '_'
----------------------------------------------------------------------
  \(                       '('
----------------------------------------------------------------------
  (                        group and capture to \3:
----------------------------------------------------------------------
    [^)]*                    any character except: ')' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \3
----------------------------------------------------------------------
  \)                       ')'
----------------------------------------------------------------------
Sign up to request clarification or add additional context in comments.

Comments

0

UPDATE @RoYoMi posted a far superior answer, but as I'd already done the work, here you go.

Since you only tagged regex here, I didn't know what language you needed to get this working in. I whipped up a JSFiddle test for this example. Here is the code.

<p>
  <input type="text" id="val" value="D:\ebook\comic\AuthorLastName_FirstName,Title_of_Book_(http://google.com).cbz" style="width:100%;" />
</p>
<div id="name"></div>
<div id="title"></div>
<div id="url"></div>
<script>
    var val = document.getElementById("val").value;

    var name = /\w+(?=[,])/.exec(val).toString().replace(/[_]/g, " ");
    var title = /\w+(?=[(])/.exec(val).toString().replace(/[_]/g, " ");

    var url = /((http)(s?)[:](\/\/))(.*?)(?=[)])/i.exec(val)[0].toString();
    //Better suited with a look behind like this:
    //var url = /(?<=[(])(.*?)(?=[)])/.exec(val).toString();
    //But JS doesn't support look behinds.

    document.getElementById("name").innerHTML = name;
    document.getElementById("title").innerHTML = title;
    document.getElementById("url").innerHTML = url;
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.