ruby


Substring after a pattern


I have a string:
str = word1.woa.1398 word2.woa.1398 word3.woa.1398 word4.woa.1398 word5.woa.1398
I want to extract "1398" for word3. I need the value of path to be just "1398". When I use:
path = str[/(?:word3.woa.*)[0-9]+/]
I get:
"word3.woa.1398 word4.woa.1398 word5.woa.1398"
Can someone provide me the correct regex?
> s = "word1.woa.1398 word2.woa.1398 word3.woa.1398 word4.woa.1398 word5.woa.1398"
> s.match(/word3.woa.(\d+)/).captures[0]
=> "1398"
str = 'word2.woa.1398 word3.woa.1398 word4.woa.1398'
str[/(?<=\sword3\.woa\.)\d+/]
#=> "1398"
(?<=\sword3\.woa\.) is a positive lookbehind, requiring \d+ to be preceded immediately by \sword3\.woa\.
You may capture the part and get it easily using the current notation as:
path = str[/\bword3\.woa\.([0-9]+)/, 1]
path
# => 1398
Here, note that the dots are escaped to match literal dots, and the 1 means we need to extract only the contents of the first capturing group.
See the Ruby demo online. Also, see this Rubular demo (pay attention to "Match groups:" section).
Note that the pattern matches:
\b - a word boundary
word3\.woa\. - a word3.woa. literal substring
([0-9]+) - 1 or more digits captured into Group 1.
Ruby Onigmo regex library supports \K (match reset operator), so you may actually just match the left-hand context, and discard it by placing \K right after it, and grab the 1+ digits as a plain match text, no need for lookbehinds/capturing groups:
path = str[/\bword3\.woa\.\K[0-9]+/]
path
# => 1398
See another Ruby demo. Here, all you matched with \bword3\.woa\. is dropped and what is matched with [0-9]+ lands in the resulting match value.

Related Links

How do you use cucumber with zeus?
How does work simple sorting in Ruby? [duplicate]
Regex dynamic string manipulation [closed]
Ruby camping microframework ActiveRecord::RecordNotFound ERROR
#dig wrapper method to accept a dynamic number of variables
Ruby 2D Extracting Information
Ruby rmagick: How to make text more bold?
How do I pass (2-∞) arguments to a ruby method?
How to get text between two divs in ruby nokogiri?
Static resource caching by configuring HTTP header in Ruby on Rails 5
I can't install RVM and Ruby on Vagrant (box “hashicorp/precise64”)
How to unit test a class that depends heavily on other classes?
How to merge values from empty rows using Ruby collection functions?
Ruby Compass error: `basename': no implicit conversion of Array into String)
Repeating a block of code until the block itself returns false?
Redis hash or key replace

Categories

HOME
influxdb
bazel
prolog
hangfire
bulkinsert
vtk
permutation
jsonp
lxc
data-structures
postman
episerver
innodb
google-visualization
http-status-code-404
torrent
wmi
virtual-reality
nhibernate
paypal-sandbox
pulp
random-forest
office-addins
snapcraft
javapos
clip
backgrid
app-engine-flexible
slice
excel-2016
getresponse
system.reactive
xodus
running-total
angularjs-scope
codeship
ebnf
json2html
afnetworking
rest-client
robotics
cascade
systemtap
pangocairo
rx.net
wsh
http-status-code-403
myget
flash-builder
ng2-datepicker
comet
restkit
olap-cube
libmemcache
specs2
welcome-file
manager
visual-sourcesafe
pushstate
watch-os
bbc-microbit
wia
metro-ui-css
rethinkdb-javascript
jscolor
inverse
hyperdex
python-packaging
socialauth
error-code
barcode4j
thread-dump
amazon-gamecircle
super
phpspec
volt
print-spooler-api
moxy
gatein
racerjs
boolean-operations
retention
session-management
toolkitscriptmanager
authorize-attribute
cmdlets
mongodb-hadoop
blueprint-css
ibatis.net
mgtwitterengine
vwdexpress
web-developer-toolbar
facebox
jaxer
virtualfilesystem
logfile-analysis
elips-studio
palm

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App