Regex issue with a string and all numbers [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In Ruby, I would like to create a regular expression that matches the following:
building/liberty-green/6d
(the word building and some number somewhere after it)
Currently, I have /building/ and need to add \d (any digit) to it, but I don't know how.

You need /building\/[\w-]+\/\w+/. For example:
irb(main):001:0> /building\/[\w-]+\/\w+/.match("building/liberty-green/6d")
=> #<MatchData "building/liberty-green/6d">
That expression will match any string that:
Starts with /building/
Then follows with one or more word characters or dashes (eg. foo-bar, foo, bar-1)
Then follows with a /
Finally ends with one or more word characters (eg. foo, 6d, 12345)
Note that \w includes digits.

Related

Extract substring from string using key pattern and delimiter using Ruby [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How can I extract substring from a string using key pattern and delimiter. e.g.
mystring = 'toto=1,2,3 mynames=ralf,john,franky myhobbies=tennis,soccer,naps'
I want to extract: ralf,john,franky
The pattern here is: mynames
The delimiter is: =
You can use this regular expression:
mynames=([^\s]+)
And then, look for the first group: $1
Here is a live example in JavaScript (works also in other languages):
var regex = /mynames=([^\s]+)/;
var text = "toto=1,2,3 mynames=ralf,john,franky myhobbies=tennis,soccer,naps"
console.log(regex.exec(text)[1]);
If you are looking for a regex which does only match everything after mynames=, so that you don't need to look for the first group, you can also use a positive lookbehind:
(?<=mynames=)[^\s]+
Here is a live example: https://regex101.com/r/mem6mA/1

Need to fetch the password using Regular expression [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
a= <<EOF
Password
:
7UV1ceFQ (You will be asked to change this after logging in for the first time)
EOF
I need to extract the value "7UV1ceFQ" using regular expression, I have tried using '/Password : 7UV1ceFQ/ but it's not working, I think it's because next line character is included, Can anyone please suggest me to exact this value?
▶ a[/^\S+(?=\s\(You will be)/]
#⇒ "7UV1ceFQ"
The regular expression above reads as:
starting with a new line start ^
get all non-space symbols greedy \S+
until the positive lookahead (?=\s\(You will be)

preg_match or preg_match_all to find all words in string with anything between them [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to find all words in a string.
The user can add words in any pattern like comma seperated or ; seperated or anything.
Input: Hello, Hi, test word
Output: array("Hello","Hi","test","word");
If a word is string that contains only word characters \w, you could do:
preg_match_all('/(\w+)/', $input_string, $matches);
Or use preg_split:
$words = preg_split('/\W+/', $input_string);

Regex for words and spaces? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to create a regex to match words and spaces:
"Hello There" - Match
"#Hello" - No Match
"123Hello" - No Match
It should something like this:
( *[a-zA-Z]* *)
I need to define a method starts_with_consonant?(s) that takes a string and returns true if it starts with a consonant and false otherwise.
For our purposes, a consonant is any letter other than 'A', 'E', 'I', 'O', 'U'.
This is for a course.
This seems to work:
(?<=^|\s)[a-zA-Z]+(\s+[a-zA-Z]+)*(?=\s|$)
See a live demo of this working with your examples (and others)
If you want to match only if the whole input matches, swap the look arounds for ^ and $:
^[a-zA-Z]+(\s+[a-zA-Z]+)*$
See a live demo of this.
I guess this does almost what you want:
\A(\w*\s+\w+)+\Z

Convert string using Ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I convert the string russ(ai)(edocn)cup to russiancodecup using Ruby?
By using gsub with a block, you can replace any match of a regular expression by the result of this block.
s = "russ(ai)(edocn)cup"
s.gsub(/\(([^)]*)\)/) {$1.reverse} # => "russiancodecup"
Here the regular expression will match any non-) character between brackets. Then it will send reverse to $1 which is gonna be the content between brackets.
$0 will be the complete match and $n, the nth "submatch". (anybody for the correct word ?)

Resources