Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex, get some match from the end

I have text like this:

Some guy writes: some content.

or

Some guy said: some content.

and i want to get all content from 'writes' to the end.

It is looking simple: (said | writes):. *

But. My text example can looks like this:

Some guy writes: blablabla, said: some content

Here i want to get only 'said: some content'. But my regex geting all content from 'writes' to the end. How to solve this problem?

like image 360
Serhiy Boreyko Avatar asked Dec 11 '25 05:12

Serhiy Boreyko


2 Answers

Prepend the regex you have with ^.* and capture it:

^.*((?:said|writes):.*)

See the regex demo

The initial .* will grab the whole line (or string if DOTALL modifier is used) and will backtrack to accommodate for said: or writes: that are the last on the string/line.

The non-capturing group in (?:said|writes) is used for grouping only purposes, so that there is only one capturing group with ID = 1.

Details:

  • ^ - start of string/line anchor (depends on the modifiers used)
  • .* - any 0+ chars as many as possible (as * is a greedy quantifier) (other than line break symbols if DOTALL modifier is not used)
  • ((?:said|writes):.*) - Group 1 capturing:
    • (?:said|writes) - either said or writes char sequences
    • : - a colon
    • .* - any 0+ chars (other than line break symbols if DOTALL modifier is not used) up to the end of line/string.
like image 52
Wiktor Stribiżew Avatar answered Dec 13 '25 20:12

Wiktor Stribiżew


I don't think without defining what the string is splitting by you could possibly do it with RegExp. You really should solidify the pattern for your string and enforce it's use, but here is a programmatic way to parse your strings.

const writes = 'Some guy writes: some content.'
const said = 'Some guy said: some content.'
const blah = 'Some guy writes: blablabla, said: some content'

function objectifyString(str) {
  const reg = /(said|writes):/
  const index = str.search(reg) || 0
  const parts = str.substring(index).split(reg).filter(item => item.length)
  const obj = {}
  const ll = parts.length
  let ii = 0
  
  for (; ii < ll; ii+=2) {
    obj[parts[ii]] = (parts[ii+1]).trim()
  }
  return obj
}

console.log(
  objectifyString(blah)
)
console.log(
  [writes, said, blah].map(objectifyString)
)
like image 40
synthet1c Avatar answered Dec 13 '25 19:12

synthet1c



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!