Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a string to match an regex?

I need to create a formatting documentation. I know the regex that are used to format the text but I don't know how to reproduce an example for that regex. This one should be an internal link:

'{\[((?:\#|/)[^ ]*) ([^]]*)\]}'

Can anyone create an example that would match this, and maybe explain how he got it. I got stuck at '?'.

I never used this meta-character at the beginning, usually I use it to mark that an literal cannot appear or appear exactly once.

Thanks

like image 216
user1236048 Avatar asked Mar 07 '12 16:03

user1236048


People also ask

How do you create a string in regex?

The Escape Symbol : \' etc characters, add a backslash( \ ) before that character. This will tell the computer to treat the following character as a search character and consider it for matching pattern. Example : \d+[\+-x\*]\d+ will match patterns like "2+2" and "3*9" in "(2+2) * 3*9".

Can regex be a string?

Definitions. In formal language theory, a regular expression (a.k.a. regex, regexp, or r.e.), is a string that represents a regular (type-3) language. Huh?? Okay, in many programming languages, a regular expression is a pattern that matches strings or pieces of strings.

Which function is used for matching regular expression with a string?

Regular expressions are used with the RegExp methods test() and exec() and with the String methods match() , replace() , search() , and split() . Executes a search for a match in a string. It returns an array of information or null on a mismatch. Tests for a match in a string.


2 Answers

(?:...) has the same grouping effect as (...), but without "capturing" the contents of the group; see http://php.net/manual/en/regexp.reference.subpatterns.php.

So, (?:\#|/) means "either # or /".

I'm guessing you know that [^ ]* means "zero or more characters that aren't SP", and that [^]]* means "zero or more characters that aren't right-square-brackets".

Putting it together, one possible string is this:

'{[/abcd asdfasefasdc]}'
like image 176
ruakh Avatar answered Nov 10 '22 00:11

ruakh


See Open source RegexBuddy alternatives and Online regex testing for some helpful tools. It's easiest to have a regex explained by them first. I used YAPE here:

NODE                     EXPLANATION
----------------------------------------------------------------------
  \[                       '['
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      \#                       '#'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      /                        '/'
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
    [^ ]*                    any character except: ' ' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
                           ' '
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    [^]]*                    any character except: ']' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  \]                       ']'
----------------------------------------------------------------------

This is under the presumption that { and } in your example are the regex delimiters.

You can just read through the list of explanations and come up with a possible source string such as:

 [#NOSPACE NOBRACKET]
like image 20
mario Avatar answered Nov 09 '22 23:11

mario