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

Regexp match time expression (mmmm:ss)
How do I create a regex in Emacs to capture text between double square brackets?
How to set a RegExp to have a real matching judgement?
Editing a line of a configuration file from shell
Perl replace the top-level numbers only from a tree
Regular Expressions match exactly 7 or 9 digits
How to accept numbers and specific words?
Javacc regular expression that matches all string except certain ones
perl regex - extract words between 2 separators
Regex number extraction
How to delete slash from the str?
how to use regex to strip the preprocessor directive
Find a string in multiple files using grep
Regex find and replace over multi lines in Shell
Grepping a 20g file in bash
What Perl matching regexp nuances can cause headaches?

Categories

HOME
performance
common-lisp
elf
ssl-certificate
compiler-errors
blogs
apacheds
styling
fpdf
normalization
go-gorm
winsql
whitespace
regression
pie-chart
proftpd
google-weather-api
condor
orchardcms-1.10
partitioning
amplitude
lookup-tables
stackexchange-api
collision
slime
windows-applications
nintex-workflow
pcap
mouseevent
selenium-edgedriver
precision
jtable
desktop-app-converter
rrd
text-parsing
monads
silverlight-5.0
stl
angular2-databinding
gridstack
jetty-9
otroslogviewer
dsx
sdcc
php-deployer
openwebanalytics
multisite
untagged
glumpy
facebook-canvas
assembly-references
telecommunication
formatter
jstorm
mars
pddl
ctakes
java-gstreamer
polygons
double-quotes
equinox
pegkit
keypad
react-rails
ehcache-bigmemory
fastparse
cordova-cli
catalina.out
first-class-functions
nhunspell
magento-1.6
jqgrid-php
validform
colormatrix
vlfeat
sysadmin
nsimageview
kmz
word-2010
hiphop
android-actionbar-compat
mysql-backup
ria
sticky-footer
hpple
self-organizing-maps
jscience
etrade-api
myisam
ryz
resharper-4.5
templatebinding

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