Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. Regex before match

Regex before match

Scheduled Pinned Locked Moved Regular Expressions
regexhelpquestion
5 Posts 3 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    moxol
    wrote on last edited by
    #1

    If I have string

    1213 494 4 toobad boys 1234 4444

    I want to match "bad boys" and everything before and after bad boys until word boundary \b. Something like this

    "\\b.+?bad boys.+?\\b"

    This returns

    1213 494 4 toobad boys

    and I want it to match "toobad boys" I know that the problem is "\\b.+?bad" which matches everything up to "bad boys", but instead it has to match everything before "bad boys" until word boundary \b. Help?

    P 1 Reply Last reply
    0
    • M moxol

      If I have string

      1213 494 4 toobad boys 1234 4444

      I want to match "bad boys" and everything before and after bad boys until word boundary \b. Something like this

      "\\b.+?bad boys.+?\\b"

      This returns

      1213 494 4 toobad boys

      and I want it to match "toobad boys" I know that the problem is "\\b.+?bad" which matches everything up to "bad boys", but instead it has to match everything before "bad boys" until word boundary \b. Help?

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      The problem is that your first \b is matching too early. Try something like [A-Za-z]*bad boys[A-Za-z]* In words, that will match "none or some alphabetic characters (no spaces!)" then "bad boys" then "none or some alphabetic characters"

      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

      M 1 Reply Last reply
      0
      • P Peter_in_2780

        The problem is that your first \b is matching too early. Try something like [A-Za-z]*bad boys[A-Za-z]* In words, that will match "none or some alphabetic characters (no spaces!)" then "bad boys" then "none or some alphabetic characters"

        Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

        M Offline
        M Offline
        moxol
        wrote on last edited by
        #3

        I can't place [A-Za-z] since there will be also a non-Latin characters. It has to be .+

        J 1 Reply Last reply
        0
        • M moxol

          I can't place [A-Za-z] since there will be also a non-Latin characters. It has to be .+

          J Offline
          J Offline
          jsc42
          wrote on last edited by
          #4

          Does \w* or \pL* or \p{L}* or (\p{L}|\p{M})* or \p{Word}* do what you want? Different RegExps support different features. See, for example, Regex Tutorial - Unicode Characters and Properties[^]

          M 1 Reply Last reply
          0
          • J jsc42

            Does \w* or \pL* or \p{L}* or (\p{L}|\p{M})* or \p{Word}* do what you want? Different RegExps support different features. See, for example, Regex Tutorial - Unicode Characters and Properties[^]

            M Offline
            M Offline
            moxol
            wrote on last edited by
            #5

            Yes, I think

            \w*bad boys\w*

            will do it. It's MySQL Regex expression, and it seems that it works for both Latin and non-Latin characters.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups