How to get a string in regex and delete other after matching the string
up vote
0
down vote
favorite
my input is following
1 blah blah blah @username_. sblah sblah sblah
the output I need is following
username_.
for now, I make this expression
^.*@([a-zA-Z0-9._]+)$
which working in following
1 blah blah blah @username_.
but if I use it for the full line it's not working
so its get the user and delete before the user
but how I can make it delete the rest once it gets the user
Note I use regex101 for testing if you have a better tool please write it below.
php regex instagram-api
add a comment |
up vote
0
down vote
favorite
my input is following
1 blah blah blah @username_. sblah sblah sblah
the output I need is following
username_.
for now, I make this expression
^.*@([a-zA-Z0-9._]+)$
which working in following
1 blah blah blah @username_.
but if I use it for the full line it's not working
so its get the user and delete before the user
but how I can make it delete the rest once it gets the user
Note I use regex101 for testing if you have a better tool please write it below.
php regex instagram-api
It will always end with _.?
– Juan Ignacio Sánchez
yesterday
You should post the actual code. And post some of the failed examples and what output they gave and what you expected.
– ryantxr
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
my input is following
1 blah blah blah @username_. sblah sblah sblah
the output I need is following
username_.
for now, I make this expression
^.*@([a-zA-Z0-9._]+)$
which working in following
1 blah blah blah @username_.
but if I use it for the full line it's not working
so its get the user and delete before the user
but how I can make it delete the rest once it gets the user
Note I use regex101 for testing if you have a better tool please write it below.
php regex instagram-api
my input is following
1 blah blah blah @username_. sblah sblah sblah
the output I need is following
username_.
for now, I make this expression
^.*@([a-zA-Z0-9._]+)$
which working in following
1 blah blah blah @username_.
but if I use it for the full line it's not working
so its get the user and delete before the user
but how I can make it delete the rest once it gets the user
Note I use regex101 for testing if you have a better tool please write it below.
php regex instagram-api
php regex instagram-api
asked yesterday
TheBlueDragon
5815
5815
It will always end with _.?
– Juan Ignacio Sánchez
yesterday
You should post the actual code. And post some of the failed examples and what output they gave and what you expected.
– ryantxr
yesterday
add a comment |
It will always end with _.?
– Juan Ignacio Sánchez
yesterday
You should post the actual code. And post some of the failed examples and what output they gave and what you expected.
– ryantxr
yesterday
It will always end with _.?
– Juan Ignacio Sánchez
yesterday
It will always end with _.?
– Juan Ignacio Sánchez
yesterday
You should post the actual code. And post some of the failed examples and what output they gave and what you expected.
– ryantxr
yesterday
You should post the actual code. And post some of the failed examples and what output they gave and what you expected.
– ryantxr
yesterday
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Your pattern uses ^$
which means it needs a full match, your pattern is only partial.
By adding a .*
it becomes a full regex and it matches as expected.
"/^.*@([a-zA-Z0-9._]+).*$/"
https://3v4l.org/i4pVd
Another way to do it is to use a partial regex like this.
It skips anything up to the @ and then captures all to a dot
$str = "1 blah blah blah @username_. sblah sblah sblah";
preg_match("/.*?@(.*?.)/", $str, $match);
var_dump($match);
https://3v4l.org/mvBYI
add a comment |
up vote
0
down vote
To match the username in you example data, you could preg_match and omit the $
to assert the position at the end of the string as in this demo. Note that you don't have to escape the @
and the dot and underscore in the character class.
To get the username in you example data, you could also use:
@K[w.]+
That would match
@
Match literallyK
Forget what was previously matched[w.]+
Match 1+ times a word character or a dot
Regex demo
$re = '/@K[w.]+/';
$str = '1 blah blah blah @username_. sblah sblah sblah @test';
preg_match($re, $str, $matches);
echo $matches[0]; // username_.
Demo php
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Your pattern uses ^$
which means it needs a full match, your pattern is only partial.
By adding a .*
it becomes a full regex and it matches as expected.
"/^.*@([a-zA-Z0-9._]+).*$/"
https://3v4l.org/i4pVd
Another way to do it is to use a partial regex like this.
It skips anything up to the @ and then captures all to a dot
$str = "1 blah blah blah @username_. sblah sblah sblah";
preg_match("/.*?@(.*?.)/", $str, $match);
var_dump($match);
https://3v4l.org/mvBYI
add a comment |
up vote
1
down vote
accepted
Your pattern uses ^$
which means it needs a full match, your pattern is only partial.
By adding a .*
it becomes a full regex and it matches as expected.
"/^.*@([a-zA-Z0-9._]+).*$/"
https://3v4l.org/i4pVd
Another way to do it is to use a partial regex like this.
It skips anything up to the @ and then captures all to a dot
$str = "1 blah blah blah @username_. sblah sblah sblah";
preg_match("/.*?@(.*?.)/", $str, $match);
var_dump($match);
https://3v4l.org/mvBYI
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Your pattern uses ^$
which means it needs a full match, your pattern is only partial.
By adding a .*
it becomes a full regex and it matches as expected.
"/^.*@([a-zA-Z0-9._]+).*$/"
https://3v4l.org/i4pVd
Another way to do it is to use a partial regex like this.
It skips anything up to the @ and then captures all to a dot
$str = "1 blah blah blah @username_. sblah sblah sblah";
preg_match("/.*?@(.*?.)/", $str, $match);
var_dump($match);
https://3v4l.org/mvBYI
Your pattern uses ^$
which means it needs a full match, your pattern is only partial.
By adding a .*
it becomes a full regex and it matches as expected.
"/^.*@([a-zA-Z0-9._]+).*$/"
https://3v4l.org/i4pVd
Another way to do it is to use a partial regex like this.
It skips anything up to the @ and then captures all to a dot
$str = "1 blah blah blah @username_. sblah sblah sblah";
preg_match("/.*?@(.*?.)/", $str, $match);
var_dump($match);
https://3v4l.org/mvBYI
edited yesterday
answered yesterday
Andreas
14.3k31440
14.3k31440
add a comment |
add a comment |
up vote
0
down vote
To match the username in you example data, you could preg_match and omit the $
to assert the position at the end of the string as in this demo. Note that you don't have to escape the @
and the dot and underscore in the character class.
To get the username in you example data, you could also use:
@K[w.]+
That would match
@
Match literallyK
Forget what was previously matched[w.]+
Match 1+ times a word character or a dot
Regex demo
$re = '/@K[w.]+/';
$str = '1 blah blah blah @username_. sblah sblah sblah @test';
preg_match($re, $str, $matches);
echo $matches[0]; // username_.
Demo php
add a comment |
up vote
0
down vote
To match the username in you example data, you could preg_match and omit the $
to assert the position at the end of the string as in this demo. Note that you don't have to escape the @
and the dot and underscore in the character class.
To get the username in you example data, you could also use:
@K[w.]+
That would match
@
Match literallyK
Forget what was previously matched[w.]+
Match 1+ times a word character or a dot
Regex demo
$re = '/@K[w.]+/';
$str = '1 blah blah blah @username_. sblah sblah sblah @test';
preg_match($re, $str, $matches);
echo $matches[0]; // username_.
Demo php
add a comment |
up vote
0
down vote
up vote
0
down vote
To match the username in you example data, you could preg_match and omit the $
to assert the position at the end of the string as in this demo. Note that you don't have to escape the @
and the dot and underscore in the character class.
To get the username in you example data, you could also use:
@K[w.]+
That would match
@
Match literallyK
Forget what was previously matched[w.]+
Match 1+ times a word character or a dot
Regex demo
$re = '/@K[w.]+/';
$str = '1 blah blah blah @username_. sblah sblah sblah @test';
preg_match($re, $str, $matches);
echo $matches[0]; // username_.
Demo php
To match the username in you example data, you could preg_match and omit the $
to assert the position at the end of the string as in this demo. Note that you don't have to escape the @
and the dot and underscore in the character class.
To get the username in you example data, you could also use:
@K[w.]+
That would match
@
Match literallyK
Forget what was previously matched[w.]+
Match 1+ times a word character or a dot
Regex demo
$re = '/@K[w.]+/';
$str = '1 blah blah blah @username_. sblah sblah sblah @test';
preg_match($re, $str, $matches);
echo $matches[0]; // username_.
Demo php
edited yesterday
answered yesterday
The fourth bird
17.9k71323
17.9k71323
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236460%2fhow-to-get-a-string-in-regex-and-delete-other-after-matching-the-string%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
It will always end with _.?
– Juan Ignacio Sánchez
yesterday
You should post the actual code. And post some of the failed examples and what output they gave and what you expected.
– ryantxr
yesterday