Finding the last specific character type in a string

user3649739

I am searching in a string constructed somewhat like this:

bla bla bla *Priorities: bla bla - yada yada - Keyword, yada *Terms

and I can capture the Keywords like so:

\*Priorities:.*?-.*?- (.*?),.*?\*Terms

However in some cases there are more or less hyphens:

bla bla bla *Priorities: bla bla - yada yada - bla bla - Keyword, yada *Terms

I can get that too with:

\*Priorities:(.*?-){1,4} (.*?),.*?\*Terms

but I am trying to avoid a second capturing group (so I don't have to modify my code). Is there anyway to do this in some way that searches from the end of the pattern match to the last .?*-? and still use the beginning of the string (*Priorities:) in the pattern match and thus avoid a second capture group?

anubhava

You can use this regex to match both input strings:

\*Priorities:.*- ([^,]*),.*?\*Terms

RegEx Breakup:

\*Priorities:  # Match literal *Priorities:
.*             # Match 0 or more any character (greedy match)
-              # Match hyphen and space
([^,]*),       # Match 0 or more of non-comma characters followed by a comma
.*?            # Match 0 or more any character (lazy match)
\*Terms        # Match literal *Terms

RegEx Demo

Note that due to lazy match .* before hyphen+space we're making sure that we match longest match before last occurrence of hyphen+space.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding ranges of specific character in a string

From Dev

Finding the last character of a file and adding a string after that?

From Dev

Finding string between 2 specific character

From Dev

Finding a Specific Character and/or String SQL Server 2008

From Java

Remove last specific character in a string c#

From Dev

Cut the string in half with the last specific character shows up in the string

From Dev

Finding "-" Character in a String in JAVA

From Dev

Finding last occurence of string

From Dev

Finding last occurence of string

From Dev

Finding a Specific Character in SQL Statement

From Dev

How to remove a specific character from a string, only when it is the first or last character in the string.

From Dev

How to remove a specific character from a string, only when it is the first or last character in the string.

From Dev

Finding the Index of a character within a string

From Dev

Trouble finding 'space' character in string

From Dev

Finding the position of a character within a string

From Dev

Finding repeated character combinations in string

From Dev

Finding the character presence in a String :java

From Dev

Finding the Newline Character "\n" in a String

From Dev

Get last character in string

From Dev

First and Last Character of a String

From Dev

Replace Last Character of String

From Dev

Delete last character of a string

From Dev

Finding last character of base file name

From Dev

Finding functions that return a specific type

From Dev

Finding beginning 0s but not the last one if it's the last character

From Dev

Finding the index of a character to match in a specific regex

From Dev

Finding filenames that end in a specific character with or without an extension

From Dev

Finding a specific string in array of strings

From Dev

Regex for finding a specific pattern in a string

Related Related

  1. 1

    Finding ranges of specific character in a string

  2. 2

    Finding the last character of a file and adding a string after that?

  3. 3

    Finding string between 2 specific character

  4. 4

    Finding a Specific Character and/or String SQL Server 2008

  5. 5

    Remove last specific character in a string c#

  6. 6

    Cut the string in half with the last specific character shows up in the string

  7. 7

    Finding "-" Character in a String in JAVA

  8. 8

    Finding last occurence of string

  9. 9

    Finding last occurence of string

  10. 10

    Finding a Specific Character in SQL Statement

  11. 11

    How to remove a specific character from a string, only when it is the first or last character in the string.

  12. 12

    How to remove a specific character from a string, only when it is the first or last character in the string.

  13. 13

    Finding the Index of a character within a string

  14. 14

    Trouble finding 'space' character in string

  15. 15

    Finding the position of a character within a string

  16. 16

    Finding repeated character combinations in string

  17. 17

    Finding the character presence in a String :java

  18. 18

    Finding the Newline Character "\n" in a String

  19. 19

    Get last character in string

  20. 20

    First and Last Character of a String

  21. 21

    Replace Last Character of String

  22. 22

    Delete last character of a string

  23. 23

    Finding last character of base file name

  24. 24

    Finding functions that return a specific type

  25. 25

    Finding beginning 0s but not the last one if it's the last character

  26. 26

    Finding the index of a character to match in a specific regex

  27. 27

    Finding filenames that end in a specific character with or without an extension

  28. 28

    Finding a specific string in array of strings

  29. 29

    Regex for finding a specific pattern in a string

HotTag

Archive