0
::head
line 1
line 2
line 3

::content
content 1
content 2
content 3   

How do I get "head" paragraph(first part) text with regex? This is from txt file.

Ozer
  • 113
  • 1
  • 9

2 Answers2

2

Unfortunately, the below doesn't work in javascript because of this: Javascript regex multiline flag doesn't work. So we have to tweak things a bit. A line break in a file can be found in javascript strings as \n. In windows this includes \r but not in linux, so our \s* becomes more important now that we're doing this without using line-ending characters ($). I also noticed that you don't need to specifically gather the other lines, since line breaks are being ignored anyway.

/(::head[^]*?)\n\s*\n/m

This works in testing in Chrome, so it should work for your needs.


this is a little fancy, but it should fit if this is used in conjunction with many similar properties.

/(::head.*?$^.*?$)^\s*$/m

Note that you need the /m multiline flag.

Here it is tested against your sample data http://rubular.com/r/vtflEgDdkY

First, we check for the ::head data. That's where we start collecting information in a group with (). Then we look for anything with .*, but we do so with the lazy ? flag. Then we find the end of the line with $ and look for more lines with data with the line start ^ then anything .*? then the line end $ this will grab multiple lines because of the multiline flag, so it's important to use the lazy matching ? so we don't grab too much data. Then we look for an empty line. Normally you just need ^$ for that, but I wanted to make sure this would work if someone had stuck a stray space or tab on the lines in between sections, so we used \s* to grab spaces. The * allows it to find "0 or more" spaces as acceptable. Notice we didn't include the empty line in the group () because that's not the data you care about.

For further reading on regex, I recommend http://www.regular-expressions.info/tutorial.html It's where I learned everything I know about regex.

Community
  • 1
  • 1
deltree
  • 3,756
  • 1
  • 30
  • 51
  • I have to use javascript... Thank for reply – Ozer Jun 08 '16 at 18:31
  • javascript uses regex natively. You want an entire app or the regular expression? – deltree Jun 08 '16 at 18:36
  • Hi again, it didnt work in JS. It returns null.. code: `var file = readTextFile("deneme.txt"); var result = file.match(/(::head.*?$^.*?$)^\s*$/m);` – Ozer Jun 08 '16 at 23:35
  • it appears to be a wierd caveat of js regex, looking into it right now. – deltree Jun 09 '16 at 02:21
  • an alternative with `\n` instead of `$^` is here http://regexr.com/3djad but I'm not seeing it work in testing either – deltree Jun 09 '16 at 02:29
1

You can use [\s\S]+::content to match everything until ::content:

const text = ...
const matches = text.match(/^([\s\S]+)::content/m)
const content = matches[1]
Alex Booker
  • 10,487
  • 1
  • 24
  • 34