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

Simon Hyttfors

I have a String that begins with a word and I want to make a substring which starts at index 0 and ends at the index of the next special character (space, ., !, ?, etc...). How would I go about doing that with a regex? Can I get the index of the first regex match? And how would the pattern look?

Thanks in advance!

hwnd

You could use the following.

^\w+(?=\W)

Explanation:

^            # the beginning of the string
\w+          # word characters (a-z, A-Z, 0-9, _) (1 or more times)
(?=          # look ahead to see if there is:
  \W         #   non-word characters (all but a-z, A-Z, 0-9, _)
)            # end of look-ahead

Example:

String s  = "foobar!";
Pattern p = Pattern.compile("^\\w+(?=\\W)");
Matcher m = p.matcher(s);

if (m.find()) {
  System.out.println("Start:" + m.start() + " End:" + m.end());
  System.out.println(m.group());
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replace a specific character in a regex match

From Dev

Regex - Exclude a match if it is preceeded by a specific character

From Dev

Regex match everything except a specific character

From Dev

Regex to match numbers followed by a specific character

From Dev

Regex match string before and after a specific character

From Dev

java regex insert character as specific index position

From Dev

Regex Match a character which is not followed by another specific character

From Dev

Finding the character "^" using regex

From Dev

Finding Index of in combination with RegEx

From Dev

Regex - Match capital, simple string after specific character

From Dev

Regex replace - match and empty all lines not containing a specific character

From Dev

how to match a character unless it is part of a specific string with regex

From Dev

Regex replace - match and empty all lines not containing a specific character

From Dev

Finding the Index of a character within a string

From Dev

Finding a Specific Character in SQL Statement

From Dev

Finding ranges of specific character in a string

From Dev

Regex - No match if trailing character is "-"

From Dev

Match a character to regex

From Dev

match first character in a regex?

From Dev

Regex - No match if trailing character is "-"

From Dev

Last Character of regex match

From Dev

Regex match " character

From Dev

Regex to match if not followed by a character?

From Dev

Finding a index of a vector with specific value

From Dev

Finding index of specific date in dateTimeArray

From Dev

Finding index of specific values in matrix

From Dev

Regex for finding a specific pattern in a string

From Dev

Regex match a specific string

From Dev

Regex Specific Match String

Related Related

  1. 1

    Replace a specific character in a regex match

  2. 2

    Regex - Exclude a match if it is preceeded by a specific character

  3. 3

    Regex match everything except a specific character

  4. 4

    Regex to match numbers followed by a specific character

  5. 5

    Regex match string before and after a specific character

  6. 6

    java regex insert character as specific index position

  7. 7

    Regex Match a character which is not followed by another specific character

  8. 8

    Finding the character "^" using regex

  9. 9

    Finding Index of in combination with RegEx

  10. 10

    Regex - Match capital, simple string after specific character

  11. 11

    Regex replace - match and empty all lines not containing a specific character

  12. 12

    how to match a character unless it is part of a specific string with regex

  13. 13

    Regex replace - match and empty all lines not containing a specific character

  14. 14

    Finding the Index of a character within a string

  15. 15

    Finding a Specific Character in SQL Statement

  16. 16

    Finding ranges of specific character in a string

  17. 17

    Regex - No match if trailing character is "-"

  18. 18

    Match a character to regex

  19. 19

    match first character in a regex?

  20. 20

    Regex - No match if trailing character is "-"

  21. 21

    Last Character of regex match

  22. 22

    Regex match " character

  23. 23

    Regex to match if not followed by a character?

  24. 24

    Finding a index of a vector with specific value

  25. 25

    Finding index of specific date in dateTimeArray

  26. 26

    Finding index of specific values in matrix

  27. 27

    Regex for finding a specific pattern in a string

  28. 28

    Regex match a specific string

  29. 29

    Regex Specific Match String

HotTag

Archive