Short Code Regex

I wrote a regex according to the shortcode in a format like:

[playername Le'Veon Bell]

      

Here's the regex:

\[playername\s+(?<name>[\w \-\'\’]+)\s*\]

      

I know that regex is struggling with one quote in the player name. I've tested the regex here and it seems to work but doesn't find a match when I run the code from my web app.

+3


source to share


4 answers


Based on the information you provided, I believe it \[playername\s+(?<name>[^]]+?)\s*\]

should work.



The only real difference is the capture group [^]]+

, which matches any character that is NOT a ]

. This will effectively grab anything after [playername

before the first ]

. It will break if you have nested parentheses.

+3


source


You can use the following regex:

\[playername\s+(?<name>.*?)\s*\]

      



Live Demo

+3


source


In this example, you do not need to hide the symbols -

, '

which are present inside a character class.

Regex rgx = new Regex(@"\[playername\s+(?<name>[\w '’-]+?)\s*\]");

      

IDEONE

+2


source


Not knowing more about your script, which is hard to say for sure, but from what you have, I would say you need to switch to something similar?

\[playername\s+(?<name>[^\]\s]*)\s+\]

      

This will match anything that doesn't have a square bracket or space in it, so it seems like what you're actually trying to do. I would personally be tempted to remove the whitespace conditions, simply on the basis that the name may have a place in it. You can always call any version of your C # language string.Trim()

after the fact.

Note, of course, that this is not exactly the same as what you are doing: you are explicitly limiting characters. But it looks like you are trying to limit any text anyway.

0


source







All Articles