capture optional surrounding characters

user4614918

I'm not that savvy in regex, so I'm not sure how to achieve the following thing:

I'd like to capture any arbitary string from an input that may or may not be surrounded by the '$' character. If a '$' character is present at the beginning of the string, the '$' character at the end must be present.

Currently I have

^\w+([_.-]\w+)*$

which roughly translates to:

  1. Arbitary word characters
  2. Beginning of capture group
  3. any character of '_', '.', '-'
  4. Before an optional \n

So valid matches would be:

test
test-5
test.1.3
test-alpha.2

Now I'd like to make this possible

$test$

But not...

$test (or test$)
vks
^(?:(?:\w+(?:[_.-]\w+)*)|\$test\$)$

Its better to add that with | instead off modifying the original one.See demo.

https://regex101.com/r/wU7sQ0/32

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does my regex capture the surrounding characters?

From Dev

Why does my regex capture the surrounding characters?

From Dev

grep surrounding characters of a match

From Dev

Caret position in textarea and surrounding characters

From Dev

How to print characters based on the surrounding characters

From Dev

capture group with optional substring

From Dev

regex with optional capture group

From Dev

How to ignore characters surrounding a URL in regex

From Dev

Ignoring cases in a regex split based on surrounding characters

From Dev

How to ignore characters surrounding a URL in regex

From Dev

Why is the capture specifier optional in capture lists?

From Dev

Regex: Optional special characters

From Dev

match optional special characters

From Dev

Optional Characters in a Lookaround

From Dev

capture last XXX characters

From Dev

Capture words that starts with characters

From Dev

Optional capture always ignored in favor of *

From Dev

how to capture multiple optional groups

From Dev

Java - Capture optional field with regexp?

From Dev

RegExp - Optional Capture group in Bash?

From Dev

optional capture groups with NSRegularExpressions in swift

From Dev

Regex replace surrounding characters while maintaining the string between

From Dev

Extracting a regex matched with 'sed' without printing the surrounding characters

From Dev

Surrounding one group with special characters in using substitute in vim

From Dev

Javascript: Regex to replace two characters and all their surrounding whitespace

From Dev

Optional Characters, Numbers, Symbols regex

From Dev

capture the starting characters with many patterns

From Dev

Javascript regular expression to capture characters

From Dev

Capture string between specific characters

Related Related

  1. 1

    Why does my regex capture the surrounding characters?

  2. 2

    Why does my regex capture the surrounding characters?

  3. 3

    grep surrounding characters of a match

  4. 4

    Caret position in textarea and surrounding characters

  5. 5

    How to print characters based on the surrounding characters

  6. 6

    capture group with optional substring

  7. 7

    regex with optional capture group

  8. 8

    How to ignore characters surrounding a URL in regex

  9. 9

    Ignoring cases in a regex split based on surrounding characters

  10. 10

    How to ignore characters surrounding a URL in regex

  11. 11

    Why is the capture specifier optional in capture lists?

  12. 12

    Regex: Optional special characters

  13. 13

    match optional special characters

  14. 14

    Optional Characters in a Lookaround

  15. 15

    capture last XXX characters

  16. 16

    Capture words that starts with characters

  17. 17

    Optional capture always ignored in favor of *

  18. 18

    how to capture multiple optional groups

  19. 19

    Java - Capture optional field with regexp?

  20. 20

    RegExp - Optional Capture group in Bash?

  21. 21

    optional capture groups with NSRegularExpressions in swift

  22. 22

    Regex replace surrounding characters while maintaining the string between

  23. 23

    Extracting a regex matched with 'sed' without printing the surrounding characters

  24. 24

    Surrounding one group with special characters in using substitute in vim

  25. 25

    Javascript: Regex to replace two characters and all their surrounding whitespace

  26. 26

    Optional Characters, Numbers, Symbols regex

  27. 27

    capture the starting characters with many patterns

  28. 28

    Javascript regular expression to capture characters

  29. 29

    Capture string between specific characters

HotTag

Archive