2

I am using this tool http://regexr.com/3fvg9 I want to mark this (weat)her in regexxr tool.

       (weath)er is good.   // i want to mark this word
       (weather is go)od.   // i want to mark this word

Please help me.

Raheel Aslam
  • 444
  • 1
  • 9
  • 28
  • Your comments say "known" word. However, a regex does not know if a word is known or not, it just matches a sequence of specific characters. I guess you do not want to match `Weather(is fine)`, right? Otherwise, [something like this would be useful](http://regexr.com/3fvgc). – Wiktor Stribiżew May 16 '17 at 06:23
  • yes, i does not want to match this words. Weather(is fine),(Weather) is good. – Raheel Aslam May 16 '17 at 06:59
  • Then you cannot use regex. Use some spelling dictionary to check matched words against to either accept or ignore the match with the regex like [this one](https://regex101.com/r/9PCacf/1) (analyze Group 1 and 2 contents). – Wiktor Stribiżew May 16 '17 at 07:02
  • @wiktor-stribiżew thanks this is working fine – Raheel Aslam May 16 '17 at 07:06

1 Answers1

1

Since there is no way to check with a regex if a word is "known" or not, I suggest extracting these parts you need first and then use a kind of a spelling dictionary to check if the words are correct. It won't be 100% accurate, but still better than pure regex.

The expression you need to extract the parts of glued words with parentheses is

(?|([a-zA-Z0-9]+)\(([a-zA-Z\s]+)\)|\(([a-zA-Z\s]+)\)([a-zA-Z0-9]+))

See the regex demo at regex101 that supports PHP regex.

The regex matches 2 alternatives inside a branch reset group inside which all capturing groups in different branches are numbered starting with the same ID:

  • ([a-zA-Z0-9]+)\(([a-zA-Z\s]+)\) - Group 1 (([a-zA-Z0-9]+)) matching 1+ alphanumeric chars, then (, and then Group 2 (([a-zA-Z\s]+)) matching 1+ letters and whitespaces and then a ) is matched
  • | - or
  • \(([a-zA-Z\s]+)\)([a-zA-Z0-9]+) - a (, then Group 1 (([a-zA-Z\s]+)) matching 1+ letters and whitespaces, ), and then Group 2 (([a-zA-Z0-9]+)) matching 1+ alphanumeric chars
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Please help this pattern. example here https://regex101.com/r/9PCacf/8 – Raheel Aslam May 17 '17 at 11:04
  • Pleas also guide this one. I want to handle single brackets {,(,[ https://regex101.com/r/9PCacf/13 – Raheel Aslam May 18 '17 at 05:47
  • @RaheelAslam: Please check https://regex101.com/r/lJrbGy/1. However, I am unsure now what you want to really match. Please narrow it down and explain what exactly you want to match in each case. and yes, it is a new question already. – Wiktor Stribiżew May 18 '17 at 07:23
  • i just want that what if user start with `(` but he/she didn't close, then i want to highlight it example: " (wheater " – Raheel Aslam May 18 '17 at 07:30
  • See [PHP regexp - Detect unclosed brackets](http://stackoverflow.com/questions/37575257/php-regexp-detect-unclosed-brackets). – Wiktor Stribiżew May 18 '17 at 07:33
  • i have understand this, but still problem here, I want to mark sentence if with start the single bracket and not ending bracket. when i match with single bracket it's matches with double brackets using regex Example 1- we(ather is fine / it should be mark 2- (weather is fine) / it should not be mark 3- wea{ther / it should be mark 4- {weather} / it should not be mark. same case for other brackets updated link https://regex101.com/r/lJrbGy/5 – Raheel Aslam May 18 '17 at 07:56
  • Please add these details to your new question, I cannot help with this one since I find it unclear/too broad. – Wiktor Stribiżew May 18 '17 at 08:00