#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    7
    Rep Power
    0

    Simple Expression Help


    I would appreciate some help with a simple expression.
    Here is a sample piece of text I want to search through.

    Line one.
    Line two: continued on.
    Line three.

    Line four: and now we continue.
    Line five.

    I want to find everything from the start of a line to a : The expression should select. Line two: Line four:
    Can someone please help me build this? I currently have ^[^:]+: but this is not working as it is finding everything up to and including the first :.
    Thanks for any help!
  2. #2
  3. Maddening Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,453
    Rep Power
    9645
    You said you want it to select "Line two:" and "Line four:", which would in fact be "everything up to and including the first :". So what's the problem?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    7
    Rep Power
    0
    Originally Posted by requinix
    You said you want it to select "Line two:" and "Line four:", which would in fact be "everything up to and including the first :". So what's the problem?
    Currently it is giving me:
    Line one.
    Line two:

    I would like it to return:
    Line two:
    Line four:

    Does that help? Thanks for the help!
  6. #4
  7. Maddening Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,453
    Rep Power
    9645
    Oh, you mean you want two results for the two lines when now it's giving you one result combining the first two lines.

    1. Modify the [^] so it also won't match the newline characters \r and \n
    2. Make sure your regex is using a "multiline" option (aka /m) which means the ^ will match the beginning of lines and not just the beginning of the input
    3. Make sure your regex is using a "global" option (aka /g), or you're doing a "match all", or you're using whatever your language offers in order to get multiple results from running the regex
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    7
    Rep Power
    0
    So should my line then read?

    ^[^:]+:/m/g
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    7
    Rep Power
    0

    Simple Express Help


    So should my line read?:
    ^[^:]+:/m/g
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    7
    Rep Power
    0
    I am not sure what you mean by "Modify the [^] so it also won't match the newline characters \r and \n
    2. Make sure your regex is using a "multiline" option (aka /m) which means the ^ will match the beginning of lines and not just the beginning of the input"

    I have tried the expression: ^[^:]+:/mg
    and it is not matching anything.
  14. #8
  15. Maddening Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,453
    Rep Power
    9645
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    7
    Rep Power
    0
    I am using Loftware's Spectrum. It uses regular java expressions to select text so it can be highlighted, bolded, etc.
  18. #10
  19. Maddening Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,453
    Rep Power
    9645
    The /m and /g thing I mentioned cannot be represented directly in a Java regular expression. Are you given any options for how to perform the match?

    As for the newline thing, please take a second to learn about regular expressions. The \r and \n metacharacters represent the two possible newline characters, so they need to be included in that negative character set you had so it will not match newline characters just like how it does not match colons.

IMN logo majestic logo threadwatch logo seochat tools logo