regex - Need regular Expression to Match the reverse of a word in a line -


sample input string: regular expression tough understand

user typed string: raluger expression si yllear tough ot understand

in above 2 string, need parse input string , user typed strings -word word , need find match in user typed string . such if

  1. regular in input string should matches raluger .
  2. is should match si
  3. really should match yllear
  4. to should match ot

note : input string not constant string , may vary. example:

another sample string can like: regex have wasted time .

user typed: xeger have wasted ym emit .

i looking regular expression match reverse of word in line.but input word may changeable .

but input string contain alphabets in both upper , lower case , along numbers in .

foreward

using regular expressions not ideal type of work. there ton of edge cases can encountered here.

description

\b([^\s])([^\s])(?:([^\s])(?:([^\s])(?:([^\s])(?:([^\s])(?:([^\s])(?:([^\s])(?:([^\s]))?)?)?)?)?)?)?\b(?=.*?\n.*?\b((?:(?:(?:(?:(?:(?:(?:\9)?\8)?\7)?\6)?\5)?\4)?\3)?\2\1)\b)

regular expression visualization

note: see image better, right click image , select view in new window.

summary

this regular expression following:

  • assumes input string composed of following 2 lines delimited new line character
    • input string
    • user created string
  • assumes words have upto 9 characters
  • assumes smallest word 2 characters long, a spelt backwards still a isn't backwards.
  • does not verify or match word location, in otherwords, word spelt backwards can appear in word position in second line.
  • compares words in first line second line, word in second line must spelled backwards match
  • capture group 0 word first line
  • capture groups 1-9 have individual letters
  • capture group 10 backwards word second line

example

live demo

https://regex101.com/r/zo4yi1/4

sample text

i'm not sure if yllear misspelt on purpose in original question, added additional incorrectly spelled raelly sample

regular expression raelly tough understand raluger expression si yllear tough ot understand 

sample matches

match 1 1.  [0-1]   `r` 2.  [1-2]   `e` 3.  [2-3]   `g` 4.  [3-4]   `u` 5.  [4-5]   `l` 6.  [5-6]   `a` 7.  [6-7]   `r` 10. [56-63] `raluger`  match 2 1.  [19-20] `i` 2.  [20-21] `s` 10. [75-77] `si`  match 3 1.  [29-30] `r` 2.  [30-31] `a` 3.  [31-32] `e` 4.  [32-33] `l` 5.  [33-34] `l` 6.  [34-35] `y` 10. [78-84] `yllear`  match 4 1.  [42-43] `t` 2.  [43-44] `o` 10. [91-93] `ot` 

explanation

node                     explanation ----------------------------------------------------------------------   \b                       boundary between word char (\w) ,                            not word char ----------------------------------------------------------------------   (                        group , capture \1: ----------------------------------------------------------------------     [^\s]                    character except: whitespace (\n,                              \r, \t, \f, , " ") ----------------------------------------------------------------------   )                        end of \1 ----------------------------------------------------------------------   (                        group , capture \2: ----------------------------------------------------------------------     [^\s]                    character except: whitespace (\n,                              \r, \t, \f, , " ") ----------------------------------------------------------------------   )                        end of \2 ----------------------------------------------------------------------   (?:                      group, not capture (optional                            (matching amount possible)): ----------------------------------------------------------------------     (                        group , capture \3: ----------------------------------------------------------------------       [^\s]                    character except: whitespace (\n,                                \r, \t, \f, , " ") ----------------------------------------------------------------------     )                        end of \3 ----------------------------------------------------------------------     (?:                      group, not capture (optional                              (matching amount possible)): ----------------------------------------------------------------------       (                        group , capture \4: ----------------------------------------------------------------------         [^\s]                    character except: whitespace                                  (\n, \r, \t, \f, , " ") ----------------------------------------------------------------------       )                        end of \4 ----------------------------------------------------------------------       (?:                      group, not capture (optional                                (matching amount possible)): ----------------------------------------------------------------------         (                        group , capture \5: ----------------------------------------------------------------------           [^\s]                    character except: whitespace                                    (\n, \r, \t, \f, , " ") ----------------------------------------------------------------------         )                        end of \5 ----------------------------------------------------------------------         (?:                      group, not capture (optional                                  (matching amount                                  possible)): ----------------------------------------------------------------------           (                        group , capture \6: ----------------------------------------------------------------------             [^\s]                    character except: whitespace                                      (\n, \r, \t, \f, , " ") ----------------------------------------------------------------------           )                        end of \6 ----------------------------------------------------------------------           (?:                      group, not capture                                    (optional (matching                                    amount possible)): ----------------------------------------------------------------------             (                        group , capture \7: ----------------------------------------------------------------------               [^\s]                    character except:                                        whitespace (\n, \r, \t, \f,                                        , " ") ----------------------------------------------------------------------             )                        end of \7 ----------------------------------------------------------------------             (?:                      group, not capture                                      (optional (matching                                      amount possible)): ----------------------------------------------------------------------               (                        group , capture \8: ----------------------------------------------------------------------                 [^\s]                    character except:                                          whitespace (\n, \r, \t, \f,                                          , " ") ----------------------------------------------------------------------               )                        end of \8 ----------------------------------------------------------------------               (?:                      group, not capture                                        (optional (matching                                        amount possible)): ----------------------------------------------------------------------                 (                        group , capture \9: ----------------------------------------------------------------------                   [^\s                     character except:                   ]                        whitespace (\n, \r, \t,                                            \f, , " ") ----------------------------------------------------------------------                 )                        end of \9 ----------------------------------------------------------------------               )?                       end of grouping ----------------------------------------------------------------------             )?                       end of grouping ----------------------------------------------------------------------           )?                       end of grouping ----------------------------------------------------------------------         )?                       end of grouping ----------------------------------------------------------------------       )?                       end of grouping ----------------------------------------------------------------------     )?                       end of grouping ----------------------------------------------------------------------   )?                       end of grouping ----------------------------------------------------------------------   \b                       boundary between word char (\w) ,                            not word char ----------------------------------------------------------------------   (?=                      ahead see if there is: ----------------------------------------------------------------------     .*?                      character except \n (0 or more times                              (matching least amount possible)) ----------------------------------------------------------------------     \n                       '\n' (newline) ----------------------------------------------------------------------     .*?                      character except \n (0 or more times                              (matching least amount possible)) ----------------------------------------------------------------------     \b                       boundary between word char (\w)                              , not word char ----------------------------------------------------------------------     (                        group , capture \10: ----------------------------------------------------------------------       (?:                      group, not capture (optional                                (matching amount possible)): ----------------------------------------------------------------------         (?:                      group, not capture (optional                                  (matching amount                                  possible)): ----------------------------------------------------------------------           (?:                      group, not capture                                    (optional (matching                                    amount possible)): ----------------------------------------------------------------------             (?:                      group, not capture                                      (optional (matching                                      amount possible)): ----------------------------------------------------------------------               (?:                      group, not capture                                        (optional (matching                                        amount possible)): ----------------------------------------------------------------------                 (?:                      group, not capture                                          (optional (matching                                          amount possible)): ----------------------------------------------------------------------                   (?:                      group, not capture                                            (optional (matching                                            amount possible)): ----------------------------------------------------------------------                     \9                       matched                                              capture \9 ----------------------------------------------------------------------                   )?                       end of grouping ----------------------------------------------------------------------                   \8                       matched                                            capture \8 ----------------------------------------------------------------------                 )?                       end of grouping ----------------------------------------------------------------------                 \7                       matched capture                                          \7 ----------------------------------------------------------------------               )?                       end of grouping ----------------------------------------------------------------------               \6                       matched capture \6 ----------------------------------------------------------------------             )?                       end of grouping ----------------------------------------------------------------------             \5                       matched capture \5 ----------------------------------------------------------------------           )?                       end of grouping ----------------------------------------------------------------------           \4                       matched capture \4 ----------------------------------------------------------------------         )?                       end of grouping ----------------------------------------------------------------------         \3                       matched capture \3 ----------------------------------------------------------------------       )?                       end of grouping ----------------------------------------------------------------------       \2                       matched capture \2 ----------------------------------------------------------------------       \1                       matched capture \1 ----------------------------------------------------------------------     )                        end of \10 ----------------------------------------------------------------------     \b                       boundary between word char (\w)                              , not word char ----------------------------------------------------------------------   )                        end of look-ahead ---------------------------------------------------------------------- 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -