regex


Powershell regex on command output for NLTEST


Running:
nltest /sc_query:domain /server:servername
Produces:
Flags: 30 HAS_IP HAS_TIMESERV
Trusted DC Name \\hostname.domain
Trusted DC Connection Status Status = 0 0x0 NERR_Success
The command completed successfully
What I want to grab into a $ is after the \\ just the 'hostname' only.
So far have: '(?<=\\\\).*' (so Expresso Regex util tells me)
This grabs everything after '\\'
How can I code this in PowerShell so I only get the hostname?
You could also use the -match operator like so:
nltest /sc_query:domain /server:servername | Where {$_ -match '\\\\([a-zA-Z0-9_-]+)'} |
Foreach {$matches[1]}
You could use:
$output = nltest /sc_query:domain /server:servername
$Hostname = ([regex]::Match($output,'(?<=\\\\).+?(?=\.)')).value
Edit: Updated Regex to get just hostname.
(?<=\\\\) = Positive look-behind for the string `\\` (It will start grab characters after the `\\`)
.+? = Any character one or more times
(?=\.) = Positive look-ahead (It will only grab characters before a `.`)
Edit 2: Realized I should make the + non-greedy so it would stop at the first .. Adjusted regex for future visitors.

Related Links

Regex to match start and end line [duplicate]
How to handle “cannot refer to an open group at position” in Python?
How to format the output of a grep with a regex pattern to match between a string and character
Regular expression to match a line containing only the list of words
How do I create a regex for 2 different patterns that outputs in 1 group, in Google sheets?
Replacing a string in between known strings over mutiple lines in Powershell
Data Preperation Identify String using Regex and move to new column
Regular expressions - optional brackets
ICU Regex matching for non-digit and no whitespace
Seemingly incorrect regex evaluation in regexp_replace
How to detect a string which does not include " using flex?
Extract certain values from string in .txt files with PowerShell
Apache 301 Redirect Add language prefix if not present
Regex, match characters and doesn't contain more than one _ followed by another _
Regex ignore word
Regex not valid in google sheets error?

Categories

HOME
opencv
selenium
dynamics-crm-365
copy
android-sqlite
apache-pig
python-3.5
design
saml
chronicle
oracle-adf
postgresql-9.5
android-dialogfragment
libssh
phpbb3
watchservice
spring-boot-admin
curve-fitting
textmatebundles
media-source
broker
sumifs
text-parsing
x-sendfile
asana-api
stl
apache-commons-httpclient
ollydbg
sieve-of-eratosthenes
gradient-descent
increment
packaging
jpype
nexus-5
couchbase-lite
nscalendar
vega-lite
multisite
html-lists
angular-directive
findall
sizing
android-sdk-tools
android-alertdialog
hyperterm
singly-linked-list
trello.net
lidar
uikeyboard
cocoon-gem
axlsx
xsb
mailx
xerces
gevent-socketio
sql-server-ce-3.5
zim-database
epplus-4
spiceworks
slick2d
jet.com-apis
private-methods
iqueryable
acts-as-taggable-on
antisamy
disabled-input
simpleaudioengine
libtorrent-rasterbar
nsusernotificationcenter
broadcom
emacs-jedi
polarssl
qdebug
git-gui
google-authorship
python-dateutil
speaker
google-goggles
haar-wavelet
blackberry-cascades
timestamp-with-timezone
android-actionbar-compat
ribbon-control
arr
maven-bundle-plugin
force.com
quilt
server.mappath
program-files
payment-services
dired
openfeint
symstore
polyglot
drwatson
virtual-server
sqlprofileprovider
mud

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