-1

Is there any other way than .match(""), to specify that my String may as well contain an empty String alongside with some other stuff? So far my regexp looks like this:

s = /(?<beginString>[\sA-Za-z\.]*)(?<marker1>[\*\_]+)(?<word>[\sA-Za-z]+)(?<marker2>[\*\_]+)(?<restString>[\sA-Za-z\*\_\.]*)/ =~ myString

now I want to assign beginString an empty String, because the program runs recursively. I want to be able to pass this function a String like "*this* _is_ ***a*** _string_" so as you can see, there is plenty possibility that beginString has to be empty in order that it reads the next marker. In the end I want my program to print this:

{"01"=>#<Textemph @content="this ">, "02"=>#<Textemph @content="is">, 
"11"=>#<Textstrongemph @content="a">, "12"=>#<Textemph @content="string">}

I think that it might fix my problem if I could just put somthing like \nil up there in the [ ]. Please just tell me how to add .match("") but in //-form.

Thank you for your Help <3

L0ren2
  • 123
  • 1
  • 1
  • 9

1 Answers1

-1

Match Start-of-String Followed by End-of-String

To match an empty string in Ruby, you could use the \A and \z atoms to match the beginning of the string immediately followed by the end of the string. For example:

"".match /\A\z/
#=> #<MatchData "">

Note that this is different from /^$/, which doesn't handle newlines as you might expect. Consider:

"\n".match /\A\z/
#=> nil

"\n".match /^$/
#=> #<MatchData "">

To properly detect an empty string, you need to use start/end of string matchers rather than start/end of line.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199